ASP.Net MVC Drop Down List -
i developing asp.net mvc 3 web application , believe method of returning list of items drop down list in view bit long winded. let me explain.
i have viewmodel contains equipment entity , selectlist display list of categories.
public class addequipmentviewmodel { public equipment equipment { get; set; } public selectlist categorylist { get; set; } }
in create method of equipment controller, return viewmodel view, see below:
//add new select list item named 'please select option' top of list var categorylist = categoryservice.getallcategories(); var categoryselectlistitems = categorylist.select(c => new selectlistitem { value = c.categoryid.tostring(), text = c.categorytitle }).tolist(); categoryselectlistitems.insert(0, new selectlistitem { text = "please select option", value = string.empty }); addequipmentviewmodel viewmodel = new addequipmentviewmodel { equipment = new equipment(), categorylist = new selectlist(categoryselectlistitems.tolist(), "value", "text") };
i know discard code before create instance of viewmodel , assign category list relevant viewmodel property so
categorylist = new selectlist(categoryservice.getallcategories(), "categoryid", "categorytitle")
however, returns list of categories drop down list in view, whereas, add new selectlistitem, ie, "please select option".
i feel approach manually adding new selectlistitem selectlist bit cumbersome , appreciate if share better method?
thanks time.
<%= html.dropdownlist("name", new selectlist(...), "your combobox default message")%>
hope helps
Comments
Post a Comment