0

I have class OrderSummaryViewModel

public class OrderSummaryViewModel
    {  
        public int Id { get; set; } 
        public string PurchaseOrderNumber { get; set; } 
        public string DeliveryCondition { get; set; } 
        /// <summary>
        /// Order items
        /// </summary>
        public List<OrderItem> Items { get; set; } 
    }

And second class OrderItem

    /// <summary>
    /// Order items
    /// </summary>
    public class OrderItem
    { 
        public int Id { get; set; } 
        public string ItemNumber { get; set; } 
        public string DeliveredQuantity { get; set; } 
        public string fullyDelivered  { get; set; } 
    }

And i bind model OrderSummaryViewModel into below .csHTML view to show the order with its related items.

      @model OrderSummaryViewModel

      <div class="baseForm">
            <label>PO Number:</label></td>
            @Html.DisplayFor(model => model.PurchaseOrderNumber) 
            <label>Delivery Condition:</label> 
            @Html.DisplayFor(model => model.DeliveryCondition) 

            @using (Ajax.BeginForm("RegisterGoodsReceipt",FormMethod.Post, new AjaxOptions { OnSuccess = "OnSuccessRegisterGoodsReceipt", OnFailure = "OnFailureRegisterGoodsReceipt" }, null))
            {
                var items= Model.Items;
                <table class="dataGrid">
                    @for (var i = 0; i < items.Count; i++)
                    {
                        var item = items[i];
                        <tr>
                            <td>@Html.DisplayFor(modelItem => item.Id)</td>
                            <td>@Html.DisplayFor(modelItem => item.ItemNumber)</td>
                            <td>
                                <input class="fullyDelivered" id="@(item.Id)" name="fullyDelivered" type="checkbox" /> 
                                <input type="text" id="deliveredQuantity@(item.Id)" name="DeliveredQuantity" value="@item.DeliveredQuantity"/>
                            </td>
                        </tr>
                    }
                </table> 
                <button type="submit"  class="button">Submit</button>
            }
     </div>

I want to submit the list of items to below action as below

public ActionResult RegisterGoodsReceipt(OrderSummaryViewModel order)
{
//to do
}

But it always go with null, i tried also to send it as list of items and it gives null also.

public ActionResult RegisterGoodsReceipt(List<OrderItem> items)
{
//to do
}

so any help regarding this issue?

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • `DisplayFor` doesn't `post` the value. You have to maintain them in `hidden fields or TextBoxFor or EditorFor` with proper indexing for list of order items. – ramiramilu Sep 16 '16 at 08:45
  • 1
    Generate your inputs correctly inside the `for` loop using `@Html.TextBoxFor(m => m.Items[i].DeliveredQuantity)` etc –  Sep 16 '16 at 08:45
  • Refer also [HTML Table to ADO.NET DataTable](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  Sep 16 '16 at 08:46
  • @ramiramilu at least it should pass deliveredQuantity textbox ?? – ahmed anwar Sep 16 '16 at 08:49
  • Not if the Id and Name of the textbox do not conform to the format expected by the MVC modelbinder. See Stephen's comment on how to generate the input correctly. – Georg Patscheider Sep 16 '16 at 08:52
  • @StephenMuecke yeah you are totally right, I thought writing HTML with same name will be enough but it seems i have to write it using Razor, thx Stephen – ahmed anwar Sep 16 '16 at 08:56
  • thx @Georg Patscheider – ahmed anwar Sep 16 '16 at 08:56
  • Note that on POST, the html sends fields based on their `name=` *not* the ID. Your C# will currently only see a single DeliveredQuantity because they all have the same `name=`. To POST to a 'deliveredquantity' list (`(List devliveredQuantity)`), they need to have different indices, eg `name='DeliveredQuantity[@i]'` - in your case they would be `name='item[@i].DevliveredQuantity'` - but always use `@Html.TextBoxFor` as already advised as this will handle it for you. – freedomn-m Sep 16 '16 at 08:57
  • @freedomn-m you are totally right thx brother – ahmed anwar Sep 16 '16 at 09:08

1 Answers1

0

By removing HTML code and writing it using Razor problem solved and it now sends the model with the right data