MVC @Html.DropDownListFor 更改 javascript 中的显示值

MVC @Html.DropDownListFor change displayed value in javascript

使用 MVC 4,我试图根据另一个下拉列表的操作更改 @Html.DropDownListFor 视图中的显示值。在下面的示例中,当用户在州列表中选择任何值时,我希望 @Html.DropDownListFor 显示索引 203 值,即美国。如何做到这一点?

//in the controller

ViewBag.statelist = new SelectList(db.states, "state_abbr", "state_name");

ViewBag.countrylist = new SelectList(db.countries, "country_id", "country_name", -1);

//in the View

<div class="form-group">
    @Html.LabelFor(model => model.states)
    @Html.DropDownList("ddlstate", ViewBag.statelist as SelectList, Model.rrq_sec1_waddr_stprov, htmlAttributes: new { @onchange="StateChange()" })
</div>

<div class="form-group">
   @Html.LabelFor(model => model.country)
   @Html.DropDownListFor(model => model.country, ViewBag.countrylist as SelectList, "Select Country", htmlAttributes: new { @id="ddlcountry" })
</div>

//javascript
function StateChange() {

    var state = document.getElementById("ddlstate").value;
    $("#ddlcountry").val = 203;
};

如何在视图中显示 "US" #ddlcountry index=203 然后状态下拉列表发生变化?

这可能会帮助您 select 使用此 jQuery 函数在第一个下拉列表的 onchange 事件中的第二个下拉列表中的值。

 $('#ddlstate').change(function () {
 $("#ddlcountry").val( $('#ddlstate').val());
});