prabhu p

prabhu p

  • 2k
  • 181
  • 98.1k

How to load a dropdown using jquery in mvc 5 Ajax call

Jun 19 2020 7:00 AM
In my scenrio dropdown is loading properly with list of records.
When click edit- dropdown want to loaded and  set the pre selected or database value want to come first
 
for ex if list as "--select--"
                           one
                          Two
                          Three
And if choose "Two"  and saved in db
 
next time dropdown should be loaded as  "Two" as default or selected index
 
 
For me every time dropdown loaded as
"--select--"
one
Two
Three
 
this is the code
 ////
$.each(response.CodeValueList, function (data, value) { 
if (value.CodeValueId == response.Currentid)// checking pre selected value with list of value
{  
$("#ddlid").append($('<option/>', { value: value.CodeValueId, selected: 'selected', html: value.Value }));
}
else {
$("#ddlid").append($('<option/>', { value: value.CodeValueId, html: value.Value }));
}
}) 
 
@Html.DropDownList("ddlid", Enumerable.Empty<SelectListItem>(), "---Select---", new { @style = "width: 100%;", @id = "ddlid" }) 
 
 Note while debugging condition satisfying  going to respective section but at last iam getting --select-- only as first item 
 
Please help on this 
                                                                

Answers (2)