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

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -