使用JQuery从MVC模型属性设置Select的选定值
我正在使用Ajax填充一个下拉列表使用JQuery从MVC模型属性设置Select的选定值,jquery,asp.net-mvc-4,Jquery,Asp.net Mvc 4,我正在使用Ajax填充一个下拉列表 $(document).ready(function() { var ID = $("#ID").val(); fillDropdown(ID); //fills the dropdown via an $.ajax call var anotherID = @Model.AnotherID; //alert(anotherID); $("#AnotherID").val(anotherID); //try to
$(document).ready(function() {
var ID = $("#ID").val();
fillDropdown(ID); //fills the dropdown via an $.ajax call
var anotherID = @Model.AnotherID;
//alert(anotherID);
$("#AnotherID").val(anotherID); //try to set the selected value
});
该下拉列表在页面中定义为:
@Html.DropDownListFor(x => x.AnotherID, Enumerable.Empty<SelectListItem>())
设置下拉列表所选值的行为:
$AnotherID.valanotherID
只有在我取消上面的警告注释时,它才会起作用
知道为什么吗?Ajax调用是异步的。您必须在Ajax调用完成后设置所选的值。警报为你赢得了时间。。。这就是它与警报一起工作的原因。如果使用jquery,请添加onsuccess回调并在回调中设置所选值