asp.net mvc - Object value is not preserved from view to controller -
i using model pass values controller model. have first controller action method :
public actionresult createadform(sitedirectionmodel model){ mymodel mymodel= new mymodel{ id = model.id, name = model.name, //an on sitedirection = model //a sitedirectionmodel object }; return view(mymodel); }
the point of method preload values , pass view form :
@model myproject.models.mymodel @using (html.beginform("validateform", "create", formmethod.post)) { @html.antiforgerytoken() <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @html.labelfor(m=>m.name, new { @class= "col-md-2 control-label" }) <div class="col-md-10"> @html.textboxfor(m => m.name, new { @class = "form-control" }) @html.validationmessagefor(m => m.name, "", new { @class = "text-danger" }) </div> </div> <input type="submit" value="créer l'utilisateur" class="btn btn-primary" />
my problem when validate form, try retrieve sitedirectionmodel sitedirection
null. wanted that, in case form not valid, return form errors shown (like textboxes not filled example), it's dysfunctional. i'm guessing values have attributed when filling form why keep getting null, have idea how this?
form validation happens after roundtrip server complete (i.e. after form post). therefore have send values server require in receiving action.
add hidden fields properties of sitedirection
need.
@html.hiddenfor(m => m.sitedirection.name) @html.hiddenfor(m => m.sitedirection.value) @* ... *@
alternatively, send id of sitedirection , load entity db again.
wiki
Comments
Post a Comment