-1
我正在爲我們的MVC應用編輯頁面,其中包含國家和州下拉菜單。我有下拉填充。國家得到加載頁面加載,並且當一個國家被選中時,狀態列表被加載。編輯時,國家/地區列表將被填充並正確選擇。MVC5編輯的國家/州下拉列表
我的問題是如何設置要選擇的狀態。這是我到目前爲止有:
<div class="form-group col-md-4">
@Html.LabelForRequired(model => model.CountryId, htmlAttributes: new {@class = "control-label"})
@Html.DropDownListFor(model => model.CountryId,
new SelectList(ViewBag.Countries as IEnumerable, "CountryId", "CountryName"),
new {@class = "form-control", id = "ddlCountry"})
@Html.ValidationMessageFor(model => model.CountryId, "", new {@class = "text-danger"})
</div>
<div class="form-group col-md-4">
@Html.LabelForRequired(model => model.State, new {@class = "control-label"})
<select id="ddlState" name="@Html.NameFor(model => model.State)"></select>
@Html.ValidationMessageFor(model => model.State, "", new {@class = "text-danger"})
</div>
這裏是我的腳本:
<script>
$(function() {
LoadStates();
$("#ddlCountry").change(function() {
LoadStates();
});
});
function LoadStates() {
var Param = { CountryId: $("#ddlCountry > option:selected").attr("value") };
$.getJSON(appFolder + "Home/StateList/", Param, function (data) {
var items = "<option>Select State</option>";
$.each(data, function (i, state) {
items += "<option value=" + state.StateCode + ">" + state.State + "</option>";
});
$("#ddlState").html(items);
});
}
</script>
我一直在尋找一個例子,但還沒有找到一個。
我喜歡這樣。 DotNetFiddle會持續多久? –