0

I have a a ViewModel that its contains List of another ViewModel Like :

 public class EditProductViewModel
{
 public string Name { get; set; }
public List<ProductPriceItemViewModel> ProductList { get; set; }
}

  public class ProductPriceItemViewModel
{
    public int PurchasePrice { get; set; }
    public int SalePrice { get; set; }
}

in View User Can Add New row and Add New Product for do this I use a jquery like this:

 $(document).on('click', '#addnewproductprice', function () {
    $("#ProductPriceList tr:last").before("<tr>"+ $("#ProductPriceList tr#template").html()+"</tr>");
});

     <tr id="template" class="hidden">
                            <td><input type="text" class="txtpurchase form-control" name="ProductList[index].PurchasePrice" /></td>
                            <td><input type="text" class="txtsale form-control" name="ProductList[index].SalePrice" /></td>
<span class="btnSavePrice btn btn-xs btn-info">Save</span>
                            </td>
                        </tr>

but when click submit and getting in Debig mode in Controller ,ProductList is null . model binder dont bind data , I dont know ? is there a way to do this?

Uthman Rahimi
  • 708
  • 5
  • 22
  • the name of the input tags `name="ProductList[index].PurchasePrice"` will not bind to the Class ProductList, The name should match the Class property name. – Rajshekar Reddy Feb 24 '16 at 07:02
  • 1
    Refer the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) –  Feb 24 '16 at 07:03
  • @StephenMuecke wow ! Thanks a lot :) – Uthman Rahimi Feb 24 '16 at 07:19
  • 1
    @UthmanRahimi, Another one [here](http://stackoverflow.com/questions/29837547/set-class-validation-for-dynamic-textbox-in-a-table/29838689#29838689) which was focused on ensuring the dynamically generated items has client side validation –  Feb 24 '16 at 07:25
  • @StephenMuecke I have a question related this topic , Can I have ask here ? – Uthman Rahimi Feb 24 '16 at 13:34
  • What is the question? –  Feb 24 '16 at 23:13
  • @StephenMuecke I have some hidden fields like this `PurchaseDetails[1457338572841].StoreDetails[1437338542886].ProductId` , it doesnt work and cant bind , but when change it to `PurchaseDetails[1457338572841].StoreDetails[0].ProductId` its work . when set ordinal numbers its work fine . why ? – Uthman Rahimi Mar 07 '16 at 08:24
  • There is nothing at all in your models relating to properties `StoreDetails` which contains a property `ProductId` so its hard to understand. But if `.StoreDetails[1437338542886].ProductId` is not binding, it suggest you do not have a hidden input for the indexer - e.g. `` –  Mar 07 '16 at 08:35
  • how can I show to you my model ? – Uthman Rahimi Mar 07 '16 at 08:37
  • Since this question is closed, I suggest you ask a new question showing the relevant models, how you are generating the html and the signature of the controller method you posting back to. But the key issue when binding collections is that the indexers must start at zero and be consecutive, **or** you must include a hidden input for the `Index` value which matches the indexer in the `name` attribute. –  Mar 07 '16 at 08:42
  • @StephenMuecke thanks , problem was `Index` – Uthman Rahimi Mar 07 '16 at 09:55

0 Answers0