简体   繁体   中英

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. 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. It will be returned as JSON. How would I bind the incoming JSON objects as name/values into the DropDownList using jQuery?

you have not posted any sample json so

...
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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