ASP.NET Razor Dropdown Selection -
i have list of string , have selected item among strings.
controller:
viewbag.groupname = new selectlist(names, names.find(s=>s==place.groupname));
view:
@html.dropdownlistfor(model => model.groupname, (ienumerable<selectlistitem>)viewbag.groupname)
but selection on view first item in list not expected. problem.
you need make sure first parameter pass html.dropdownlistfor
set value of selectlistitem
should selected. if value of doesn't match values in dropdownlist, won't set item selected.
in case, need make sure model.groupname
set value of selectlistitem should selected.
example:
.cs:
class myviewmodel { public string selectedvalue = "3"; public list<selectlistitem> listitems = new list<selectlistitem> { new selectlistitem { text = "list item 1", value = "1"}, new selectlistitem { text = "list item 2", value = "2"}, new selectlistitem { text = "list item 3", value = "3"} }; }
.cshtml:
@model myviewmodel @html.dropdownlistfor(m => m.selectedvalue, model.listitems)
Comments
Post a Comment