MetaGuru
MetaGuru

Reputation: 43863

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?

Upvotes: 0

Views: 412

Answers (1)

Rafay
Rafay

Reputation: 31043

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

Upvotes: 2

Related Questions