c# - MVC Model binding issue - list of objects -


the scenario shopping basket - user update text box quantity , hit update button. currently, upon hitting update button, action hit model properties null.

my first thoughts there issue binding because ids of controls changed each row.

are thoughts correct? if so, how can resolve this?

edit: tried use loop, rather foreach didn't work either. code below:

<table class="order-table order-table-nomargin order-table-noborder hidden-xs hidden-sm">   <thead>     <tr><th>products</th><th></th><th>price</th><th>qty.</th><th>total</th></tr>   </thead>   <tbody>     @foreach (nop.web.models.custom.customorderline ol in model.cart.orderlines)     {           @html.displayfor(m => ol)     }         </tbody> </table>  

this displayfor template:

@model nop.web.models.custom.customorderline <tr>     <td>         <img alt="book image" src="@model.image.media.url"  width="100" />     </td>     <td>         @html.actionlink(@model.title, "product", "catalog", new { sename = @model.sename }, null)<br />         @*@html.displayfor(m => m.title)<br />*@ by: @html.displayfor(m => m.author)<br />         published date: @html.displayfor(m => m.publisheddate)<br />         format: @html.displayfor(m => m.format)     </td>     <td>         <p class="onlineprice">             <em>@html.displayfor(m => m.pricewithdiscount)</em></p>         <p class="saving">             <em>@html.displayfor(m => m.pricediscount)</em></p>         <p class="rrp">             rrp: <em>@html.displayfor(m => m.price)</em></p>     </td>     <td>         @using (html.beginform("updatecart", "customcart"))         {              string test = model.isbn13;             int quanttest = model.qty;              @html.editorfor(m => model.qty)             @html.hiddenfor(m => m.isbn13)             //@html.textboxfor(m => m.qty)             <input type="submit" value="update" />         }     </td>     <td>         <p class="subtotal numeric-right">@html.displayfor(m => m.total)</p>     </td> </tr> 

my controller action:

[httppost] public actionresult updatecart(customorderline model) {      //add code here       return xxx; } 

here's generated html using foreach loop:

<table class="order-table order-table-nomargin order-table-noborder hidden-xs hidden-sm"> <thead> <tr><th>products</th><th></th><th>price</th><th>qty.</th><th>total</th></tr> </thead> <tbody> <tr>     <td>         <img alt="book image" src="http://media.harrypotter.bloomsbury.com/rep/s/9781408855652_309039.jpeg"  width="100" />     </td>     <td>         <a href="/uk/harry-potter-and-the-philosophers-stone-9780747558194-9781408855652">harry potter , philosopher&#39;s stone </a><br />          by: j.k. rowling<br />         published date: 01-09-2014<br />         format: paperback     </td>     <td>         <p class="onlineprice">             <em>6.29</em></p>         <p class="saving">             <em>save ВЈ0.70 (10%)</em></p>         <p class="rrp">             rrp: <em>6.99</em></p>     </td>     <td>  <form action="/customcart/updatecart" method="post"> <input class="text-box single-line" data-val="true" data-val-number="the field qty must number." data-val-required="&amp;#39;qty&amp;#39; must not empty." id="ol_qty" name="ol.qty" type="text" value="1" /> <input id="ol_isbn13" name="ol.isbn13" type="hidden" value="9781408855652" />             <input type="submit" name="123" id="123" value="update 2" /> </form>    </td>     <td>         <p class="subtotal numeric-right">6.29</p>     </td> </tr> </tbody> </table>  

first of all, if want multiple customorderlines received in controller action, you're going need take list of items. in case, seem it's more appropriate take cart, has list of customorderlines on it:

[httppost] public actionresult updatecart(cart cart) {     //add code here     return xxx; } 

mvc model binding populates lists parameters this:

cart.orderlines[0].itemid = 123 cart.orderlines[0].qty = 1 cart.orderlines[1].itemid = 456 cart.orderlines[1].qty = 2 

so in order name attributes of html inputs match pattern, editorfor needs given expression know how construct name:

@for (int i=0; i<model.cart.orderlines.length; i++) {       @html.editorfor(m => m.cart.orderlines[i]) }   

you'll notice i'm using editorfor, since appears you're showing editable version of orders. if this, want move display template location editor templates instead.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -