Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JQuery从MVC模型属性设置Select的选定值_Jquery_Asp.net Mvc 4 - Fatal编程技术网

使用JQuery从MVC模型属性设置Select的选定值

使用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

我正在使用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 set the selected value

});
该下拉列表在页面中定义为:

@Html.DropDownListFor(x => x.AnotherID, Enumerable.Empty<SelectListItem>())
设置下拉列表所选值的行为: $AnotherID.valanotherID

只有在我取消上面的警告注释时,它才会起作用


知道为什么吗?

Ajax调用是异步的。您必须在Ajax调用完成后设置所选的值。警报为你赢得了时间。。。这就是它与警报一起工作的原因。如果使用jquery,请添加onsuccess回调并在回调中设置所选值