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

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -