c# - ViewModel does not update Model correctly on submit -
i trying use strongly-typed viewmodel , find when replace model class viewmodel, changes not submitted correctly edit template. whereas using straight model class in view, edits happen successfully. model class materialdefinition , viewmodel class materialdefinitionviewmodel shown below. i've updated edit template correctly reference viewmodel editing not work. i'm using vs2013 , mvc4. ideas, someone?
first viewmodel class...
public class materialdefinitionviewmodel { // properties public materialdefinition definition { get; private set; } // constructor public materialdefinitionviewmodel(materialdefinition def) { definition = def; } }
and code view...
<div class="editor-field"> @html.editorfor(model => model.definition.mddescription) @html.validationmessagefor(model => model.definition.mddescription) </div> <p> <input type="submit" value="save" /> </p>
finally discovered answer after searching...
[httppost] public actionresult edit(string id, formcollection collection) { materialdefinition def = repository.getmaterialdefinition(id); updatemodel(def, "definition"); //updatemodel(def); repository.save(); return redirecttoaction("details", new { id = def.mdid }); }
turns out there not-so-obvious overload updatemodel method takes prefix "name" property. name of original encapsulated model class inside viewmodel. corrected code fragment controller's edit post method shown above.
Comments
Post a Comment