asp.net mvc 3 - How to get rid of DropDownListFor MVC3 -


problem: how rid of event drop down?

when comment out in view model validation errors. doesn't know event associate with. though i'd told in create action - shown because has selected correct drop down option.

enter image description here

 public actionresult create(guid idevent)     {         viewbag.eventname = uow.events.single(e => e.id == idevent).name;          raceviewmodel racesviewmodel = new raceviewmodel();         setupdropdownsstronglytyped(racesviewmodel);          race race = new race();         racesviewmodel.raceinvm = race;          event _event = uow.events.single(e => e.id == idevent);         racesviewmodel.raceinvm.eventu = _event;          return view(racesviewmodel);     } 

view

<div class="editor-field"> @html.dropdownlistfor(x => x.raceinvm.eventuid, new selectlist(model.listofevents, "id", "name")) 

post:

 [httppost]     public actionresult create(raceviewmodel raceviewmodel, guid idevent)     {         if (!modelstate.isvalid)         {             setupdropdownsstronglytyped(raceviewmodel);             return view(raceviewmodel);         }          uow.add(raceviewmodel.raceinvm);         uow.savechanges();         return redirecttoaction("index");     } 

if understand correctly (and i'm not sure do), you're confused why racesviewmodel.raceinvm.eventu isn't populated on post action, though set in action? if so, answer , post separate actions far server-side local variables concerned - state "maintained" across calls whatever round-tripped down client , server, happens when explicitly put form elements in view cause data included in html , posted server. solution either replace html.dropdownfor html.hiddenfor, or else repopulate variable in post action (in case i'm not sure you'd need populate in action @ all.)


Comments