asp.net mvc - Using one model to grab multiple variations of data -


i have model ipacs_master_list complete mapping of table in database named ipacs_master_list.

here controller:

public actionresult index()         {             var docs = db.ipacs_master_list;              return view(docs);         } 

in view displaying entire table. on view trying add dropdownlist populated each department.

i need pass entire table default view page being able see in table. having way trouble trying grab distinct list of departments , throw dropdownlist. have tried many different ways , keep running brick wall. @ point take method works.

also understand new mvc, linq , razor, explanation appreciated.

the default view works perfectly, displays information correctly. cannot seem populate dropdownlist.

----- edit #1 ----

here way grab distinct list of departments. not sure if correct way, provide in hope helps.

    var departments = db.ipacs_master_list.where(x => (x.department != null))                .select(s => new selectlistitem                 {                     value = s.department,                     text = s.department                 })                 .distinct(); 

in model im assuming u have this:

public class yourmodel{  public ienumerable<selectlistitem> departments{get;set;}  public string selecteddepartment{get;set;}  } 

in view should have render

@html.dropdownlistfor(model => model.selecteddepartment, new selectlist(@model.departments, "key", "value", model.selecteddepartment))   

in controller have this

yourmodel model=new yourmodel(); model.departments = db.ipacs_master_list.where(x => (x.department != null))                .select(s => new selectlistitem                 {                     value = s.department,                     text = s.department                 })                 .distinct(); return view(model); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -