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

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -