如何使用MVC在下拉列表选择中显示文本框中的数据

时间:2014-10-20 13:09:12

标签: asp.net-mvc asp.net-mvc-3

我是MVC的新手。我试图在下拉列表中选择值时在文本框中显示数据。

代码"创建视图":

    @model CRUD_using_LinQ_to_SQL_in_MVC.Models.OtdModelClass
@{
ViewBag.Title = "Create";}
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Create</legend>

    <div class="editor-label" style="font-weight: bold">
        Server Type:
    <//div>
    <div>
    <select name="Server Type" id="ServerType">
        <option>Select</option>
        <option value="1">Internet</option>
        <option value="2">Lease</option>
    </select>
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.MemberCode)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.MemberCode, new { maxlength="6"})
        @Html.ValidationMessageFor(model => model.MemberCode)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.LoginID)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.LoginID, new { maxlength = "6" })
        @Html.ValidationMessageFor(model => model.LoginID)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.OTDPassword)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.OTDPassword, new { maxlength = "7" })
        @Html.ValidationMessageFor(model => model.OTDPassword)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.BBSID)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.BBSID, new { maxlength = "6" })
        @Html.ValidationMessageFor(model => model.BBSID)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.IPAddress)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.IPAddress)
        @Html.ValidationMessageFor(model => model.IPAddress)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.ServerType)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ServerType)
        @Html.ValidationMessageFor(model => model.ServerType)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.OTDStatus)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.OTDStatus)
        @Html.ValidationMessageFor(model => model.OTDStatus)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.RemoteIP)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.RemoteIP)
        @Html.ValidationMessageFor(model => model.RemoteIP)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.RemotePort)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.RemotePort)
        @Html.ValidationMessageFor(model => model.RemotePort)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.AllowDownload)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.AllowDownload)
        @Html.ValidationMessageFor(model => model.AllowDownload)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.OTDTimeStamp)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.OTDTimeStamp)
        @Html.ValidationMessageFor(model => model.OTDTimeStamp)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.MemberType)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.MemberType)
        @Html.ValidationMessageFor(model => model.MemberType)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.EQ)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.EQ)
        @Html.ValidationMessageFor(model => model.EQ)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.EQD)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.EQD)
        @Html.ValidationMessageFor(model => model.EQD)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.BFX)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.BFX)
        @Html.ValidationMessageFor(model => model.BFX)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.SLB)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SLB)
        @Html.ValidationMessageFor(model => model.SLB)
    </div>
    <div class="editor-label" style="font-weight: bold">
        @Html.LabelFor(model => model.Others)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Others)
        @Html.ValidationMessageFor(model => model.Others)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>}<div>    @Html.ActionLink("Back to List", "Index")</div>

当用户选择选项&#34; Internet&#34;在服务器类型然后&#34;远程IP&#34;和#34;远程端口&#34;字段应使用相应的值自动填充。 我在互联网上搜索了很多,但我没有得到适当的解决方案。 你能帮我么? 提前谢谢......

2 个答案:

答案 0 :(得分:0)

那是因为你在HTML中编写了自己的代码。您应该使用的是MVC中的DropDownListFor方法。

This answer可以为您提供示例。

答案 1 :(得分:0)

请尝试以下,
1.为您提到的下拉列表创建一个jquery onchange事件 2.获取所选值 3.使用Json返回类型和输入参数创建一个新的操作方法,作为名为ServerType的字符串。并查询数据库以根据服务器类型获取RemoteIp和Remoteport值 4.在jquery onchange事件中,对步骤3中创建的action方法执行ajax调用。
5.将json数据形成为“{”ServerType“:”您选择的下拉值“} 6.在ajax成功方法中,为文本框分配成功方法结果中的相应数据。

希望您知道使用jjery更好地使用ajax调用。您可以使用Html.Dropdownfor()控件作为另一个答案建议。如果需要任何信息,请告诉我。