asp.net mvc - DropDownList From Objects MVC -


ok want make dropdownlist object list here getting object method

public list<category> getcategorieslist() {     baseshopentities context = new baseshopentities();     list<category> uniqcategories = (from c in context.category                                              select c).distinct().tolist<category>();      return uniqcategories; } 

i trying put viewbag this:

viewbag.categories = proxy.getcategorieslist().tolist(); 

if im taking names of categorys , parsing string there no problem need id too

i want data don't know how

@html.dropdownlist("categoryid", new selectlist(viewbag.categories.name)); 

firstly, getcategorieslist returns list, don't need tolist when add viewbag:

viewbag.categories = proxy.getcategorieslist(); 

secondly, in order create dropdownlist has independent names , values, need following overload of selectlist constructor:

selectlist(ienumerable, string, string) 

here, specify list populate selectlist 2 strings, first name of value field of elements in list , second name of text field. values of these fields retrieved using reflection. in case, it'll this:

@html.dropdownlist("categoryid",     new selectlist(viewbag.categories, "id", "name")); 

here, assuming each category has id field , name field. if these field names aren't correct, you'll need change strings passed selectlist constructor accordingly.

on side note: should create separate view model categories separate database entity class , create list of these instead. views shouldn't accessing data entities directly.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -