ASP.net MVC Listbox -
i have number of html.listbox controls on view being populated ienumberable<selectlistitem>
is there way when data populated, items automatically selected?
the particular selectlist items in ienumerable marked selected = true, yet not convey view.
the view such:
<%= html.listbox("projects", model.projects)%>
thanks.
sure start defining view model represent information show on particular view:
public class myviewmodel { public string[] selecteditems { get; set; } public ienumerable<selectlistitem> items { get; set; } }
then controller:
public class homecontroller : controller { public actionresult index() { var model = new myviewmodel { selecteditems = new[] { "1", "3" }, items = new[] { new selectlistitem { value = "1", text = "item 1" }, new selectlistitem { value = "2", text = "item 2" }, new selectlistitem { value = "3", text = "item 3" }, } }; return view(model); } }
and in typed view:
<%= html.listboxfor( x => x.selecteditems, new selectlist(model.items, "value", "text") ) %>
as expected item 1 , item 3 preselected when list box rendered.
Comments
Post a Comment