asp.net mvc - MVC4 Pass custom view Model back to Controller Action -
i have custom class
public class bloggerviewmodel { public person blogger; public list<bloggerwebsite> bloggerwebsites; }
that pass view
[httpget] public actionresult edit(int id) { blogger = getbyid(id); var websites = getbloggerswebsites(id); var bloggerviewmodel = new bloggerviewmodel(); bloggerviewmodel.blogger = blogger; bloggerviewmodel.bloggerwebsites = websites; return view(bloggerviewmodel); }
then when post edit action
[httppost] public actionresult edit(bloggerviewmodel entity) { return view(entity); }
entity
null.
my view ( took out allot of code didn't need see. such of text box bindings)
@model bloginect.model.models.bloggerviewmodel <h2>edit</h2> @using (html.beginform()) { @html.validationsummary(true) <fieldset> <legend>person</legend> @html.hiddenfor(model => model.blogger.id) <div class="editor-label"> @html.labelfor(model => model.blogger.firstname) </div> <div class="editor-field"> @html.editorfor(model => model.blogger.firstname) @html.validationmessagefor(model => model.blogger.firstname) </div> <div class="editor-label"> @html.labelfor(model => model.blogger.lastname) </div> <div class="editor-field"> @html.editorfor(model => model.blogger.lastname) @html.validationmessagefor(model => model.blogger.lastname) </div> <div class="editor-field"> @if (model.bloggerwebsites[0].website != null) { html.editorfor(model => model.bloggerwebsites[0].website); } else { @html.textbox("website1") } </div> <div class="editor-field"> @if (model.bloggerwebsites[1].website != null) { html.editorfor(model => model.bloggerwebsites[0].website); } else { @html.textbox("website2") } </div> <div class="editor-field"> @if (model.bloggerwebsites[2].website != null) { html.editorfor(model => model.bloggerwebsites[0].website); } else { @html.textbox("website3") } </div> <p> <input type="submit" value="save" /> </p> </fieldset> } <div> @html.actionlink("back list", "index") </div>
is there doing wrong? have checked out of other replies similar questions on , did not answer question.
you must use editors handle bindings nested model properties, example on how can found here.
Comments
Post a Comment