save - saving a model from a strongly typed view -


imagine have sent model view... attempting save model once edit it. if don't write out of fields (that identity object instance), somehow reset 0 or empty (if string). did though, write out hidden field when attempt save object, able identify object is...

is form? or missing step?

if you've specified model type view on header of file, , using html.beginform helper method, i'm pretty sure take care of sending id you.

edit: tested it, , it's right. html.beginform method created output

<form action="/product/edit/1" method="post"> 

that's why sends id.

here's controller used test it:

using system.web.mvc; using mvcapplication2.models;  namespace mvcapplication2.controllers {     public class productcontroller : controller     {        public actionresult edit(int id)         {                         return view(new product { id = 1, name = "test"});         }          [httppost]         public actionresult edit(product product)         {             return edit(product.id);         }      } } 

and view:

@model mvcapplication2.models.product  @using (html.beginform()) {     <fieldset>         <legend>product</legend>          <div class="editor-label">             @html.labelfor(model => model.name)         </div>         <div class="editor-field">             @html.editorfor(model => model.name)             @html.validationmessagefor(model => model.name)         </div>          <p>             <input type="submit" value="save" />         </p>     </fieldset> } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -