Here is what I have:
for (i = 0; i < data.length; i++) {
$("#myDropDownLisTId").find('tbody')
.append($('<option>').attr('value', data[i].id).attr('name', data[i].name)
);
}
But the dropdown list always remains the same. Why is this not working? the data variable has beautiful values.
Also, I would like before the for loop to empty the dropdown list/ How to do it?
Here is my drop down list in the view:
<%= Html.DropDownList("myDropDownLisTId")%>
Try this.
Check my this answer for more details about how to get data from an MVC action to load the dropdown list
EDIT: As per the comment to remove existing data
EDIT 2:
Idrumsgood‘s answer has a very good point. Instead of calling the append method everytime inside the loop, just keep the value inside a variable and call the html method only once.
http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-correctly