c# - Form data sent from the front end has different result in controller ASP.NET MVC -


hi having trouble updating product using entity framework. when submitted form, developer's tools, can confirm form sending [0].id = 3.

but when debugging model, debug.print keeps printing 0 id.

here controller

public actionresult details(ilist<item> model) { if (modelstate.isvalid)         {             debug.print(model[0].id.tostring()); //this prints 0             debug.print(model[0].name);// prints correctly             debug.print(model[0].size);// prints correctly             debug.print(model[0].retailprice.tostring());// prints correctly             debug.print(model[0].isavailable.tostring());// prints correctly             (int = 0; < model.count(); i++)             {                 ndb.entry(model[0]).state = system.data.entity.entitystate.modified;                 ndb.savechanges();             }              return redirecttoaction("productindex");         }         return view(model);     } 

anyone has idea why?

edit:

using (html.beginform()) { @html.antiforgerytoken()  <table>     <thead>         <tr>             <td>name</td>             <td>size</td>             <td>price</td>             <td>available</td>         </tr>     </thead>     @for (int = 0; < model.count(); i++)     {         <tr>             @html.hiddenfor(m => m[i].id)             @html.hiddenfor(m => m[i].name)             @html.hiddenfor(m => m[i].size)             @html.hiddenfor(m => m[i].retailprice)             <td>                 @model[i].name             </td>             <td>                 @model[i].size             </td>             <td>                 @model[i].retailprice             </td>             <td>                 @html.checkbox("["+i+"].isavailable", model[i].isavailable)             </td>         </tr>     }      <tr>         <td>             <input type="submit" value="save" class="btn btn-default" />         </td>     </tr>  </table> } 

and have mentioned earlier, form submit developer's tool showed correct id, when came controller, gives me 0 id.

you missed id field in view. should add id field in view hidden field or bind property.

it's not recommended use ef objects in view. create custom view model instead.


Comments