c# - how do i select value from dropdownlist to mode or to controller mvc -


model

public class company {     public int id { get; set; }     ....     public int workzoneid { get; set;  }     public ienumerable<selectlistitem> workzones { get; set; } 

controller:

viewbag.workzones = cmprep.getworkzonedrop(); 

repository:

public list<selectlistitem> getworkzonedrop() {     sqldatareader dr;     dr = ado.execdatareaderproc("workzoneselectactive");     list<selectlistitem> selectworkzonelistitems = new     list<selectlistitem>();     companyvmlist c = new companyvmlist();     c.companies = new list<company>();     while (dr.read())     {         selectlistitem selectlistitem = new selectlistitem         {             text = convert.tostring(dr["name"]),             value = convert.tostring(dr["id"]),         };         selectworkzonelistitems.add(selectlistitem);     }     return selectworkzonelistitems; } 

view

@html.dropdownlist("workzones") 

i want assign selected dropdownlist value int work zone int mode how select value dropdownlist model or controller

you need bind dropdownlist property workzoneid

@html.dropdownlistfor(m => m.workzoneid, model.workzones) 

and if wanting preselect option, need set value in controller before pass model view. , since model contains property selectlist, use (don't use viewbag)

company model = new company {     workzoneid = 1 // set value matches 1 of options     workzones = cmprep.getworkzonedrop(); }; return view(model); 

and when submit to

[httppost] public actionresult edit(company model) 

the value of model.workzoneid contain value of selected option.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -