c# - Populate select box from database using MVC -


i creating website using asp.net , mvc. need populate select box values database , wondering how go doing this?

there many different ways it, do. in service class, db select & return selectlist object

public selectlist getasselectlist()         {             var depts = d in getall()                         select new                         {                             id = d.id,                             name = d.name                         };              return new selectlist(depts, "id", "name");         } 

then in model, assign selectlist property:

model.suppliers = _supplierservice.getasselectlist();  return view(model); 

and view:

@html.dropdownlistfor(model => model.deliverystoreid, model.deliverystores) 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -