简体   繁体   English

在razor / jquery中通过AJAX(AJAJ?)填充下拉列表的理想方法是什么?

[英]What's the ideal method for populating a drop down list via AJAX (AJAJ?) in razor/jquery?

So I've got this @Html.DropDownList("Thangs") inside of a hidden div. 所以我在隐藏的div里面得到了这个@Html.DropDownList("Thangs") When the user clicks a link to expose the div, I want to make an AJAX call to grab the things that should be in the list. 当用户单击链接以显示div时,我想进行AJAX调用以获取应该在列表中的内容。 It will be returned as JSON. 它将作为JSON返回。 How would I bind the incoming JSON objects as name/values into the DropDownList using jQuery? 如何使用jQuery将传入的JSON对象作为名称/值绑定到DropDownList中?

you have not posted any sample json so 你没有发布任何样本json所以

...
success:function(data){

$("#SelectList").empty(); //remove previous items in the ddl

//iterate the json
$.each(data,function(key,val){
  $("<option/>",{value:key,text:val}).appendTo("#SelectList");
  });
}

here is the DEMO 这是DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM