Asp.net mvc 如何使用MVC在下拉选择时在文本框中显示数据

Asp.net mvc 如何使用MVC在下拉选择时在文本框中显示数据,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,我对MVC非常陌生。我试图在下拉列表中选择值时在文本框中显示数据 创建视图的代码: @model CRUD_using_LinQ_to_SQL_in_MVC.Models.OtdModelClass @{ ViewBag.Title = "Create";} 在这里,当用户在服务器类型中选择“Internet”选项时,远程IP和远程端口字段应自动填充相应的值。 我在网上搜索了很多,但没有找到合适的解决办法。 你能帮帮我吗? 提前感谢…这是因为您自己正在用HTML编写下拉列表。您应该使用

我对MVC非常陌生。我试图在下拉列表中选择值时在文本框中显示数据

创建视图的代码:

    @model CRUD_using_LinQ_to_SQL_in_MVC.Models.OtdModelClass
@{
ViewBag.Title = "Create";}
在这里,当用户在服务器类型中选择“Internet”选项时,远程IP和远程端口字段应自动填充相应的值。 我在网上搜索了很多,但没有找到合适的解决办法。 你能帮帮我吗?
提前感谢…

这是因为您自己正在用HTML编写下拉列表。您应该使用的是MVC中的方法

可以为您提供一个示例。

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

希望您更好地了解使用jquery和ajax调用。正如另一个答案所建议的,您可以使用Html.dropdown进行控制。如果需要任何信息,请告诉我。

可能的副本
@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>