如何使用MVC .net绑定列表?

时间:2016-09-16 08:43:35

标签: asp.net-mvc asp.net-mvc-4

我上课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; } 
    }

第二课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; } 
    }

我将模型OrderSummaryViewModel绑定到下面的.csHTML视图中,以显示订单及其相关项目。

      @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>

我想将项目列表提交到以下操作,如下所示

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

但是它总是带有null,我也尝试将它作为项目列表发送,它也给出了null。

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

所以关于这个问题的任何帮助?

1 个答案:

答案 0 :(得分:0)

通过删除HTML代码并使用Razor解决问题,现在它发送带有正确数据的模型