drop down menu - How to post the selected value of a selectlist to the controller using a view model? -
this question has been asked in various forms none of answers seem fit situation. trying retrieve selected value of dropdown list in controller.
here code:
viewmodel.cs
public class viewmodel {   public viewmodel() {}   public viewmodel(contact contact, ienumerable<state> states)     {       this.contact = contact;       this.states = new selectlist(states, "id", "name", contact.stateid);     }   public contact contact {get;set;}   public selectlist states {get;set;} }   controller.cs
[httppost] public actionresult edit(viewmodel viewmodel) {   _contactservice.updatecontact(viewmodel.contact);   return redirecttoaction("item", new {id = viewmodel.contact.id}); }   view.cshtml
<button type="submit" onclick="javascript:document.update.submit()"><span>update</span></button>//aesthic usage.  @{using (html.beginform("edit", "controller", formmethod.post, new { name = "update" }))   {    @html.hiddenfor(m => m.contact.id)    @html.labelfor(m => m.contact.name, "name:")    @html.textboxfor(m => m.contact.name)     <label for="state">state:</label>    @html.dropdownlist("state", model.states) } }   everything works expected except no values dropdownlist passed in posted viewmodel controller. edit page , fields load correctly. dropdowns bind correctly , have selected values displayed properly. however, when post "contact" object passed controller. "states" selectlist object null.
i tried mapping "stateid" property in viewmodel contstructor did not work either. doing wrong?
thanks.
i hate answering own questions based on multiple issues had coupled myriad of answers available thought summarize findings.
first off filip, answer did not fix problem led me in right direction. +1
if creating form viewing , editing requires drop down list, here suggestions , gotchas. start list of parameters needed fit needs.
- strongly typed views in view preferable. minimize magic strings.
 - view models should contain little logic , extraneous elements possible. there job should facilitate collection of data objects.
 - the drop down list should display selected value.
 - the selected value should map view model on form submit.
 
this may sound obvious , obtainable list new mvc, not. revise code above comments. here did.
viewmodel.cs
public class viewmodel {   public viewmodel() {}   public viewmodel(contact contact, ilist<state> states)   { //no need pass in selectlist or ienumerable, service or repository spits out     this.contact = contact;     this.states = states;   }   public contact contact {get;set;}   public ilist<state> states {get;set;} }   controller.cs //nothing different above
public actionresult edit(int id) {   var contact = _contactservice.getcontactbyid(id);   var states = _stateservice.getallstates();   return view(new viewmodel(contact, states)); }  public actionresult edit(viewmodel viewmodel) {   _contactservice.updatecontact(viewmodel.contact);   return redirecttoaction("edit", new {id = viewmodel.contact.id }); }   view//thanks goes artirto @ post
@{using (html.beginform("edit", "controller", formmethod.post))  {   @html.hiddenfor(m => m.contact.id)   @html.dropdownlistfor(m => m.contact.stateid, new selectlist(model.states, "id", "name", @model.contact.stateid)) <input type="submit" value="save" /> } }      
Comments
Post a Comment