绑定数组错误

时间:2013-09-06 10:01:25

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

我目前在我的系统中有一个列表视图,其中显示了html输入列表和一个创建按钮。我想为每一行输入创建一个Customer返回项。因此它非常复杂。我尝试了以下操作,并收到错误:

  

"对象引用未设置为对象的实例。

     

描述:执行期间发生了未处理的异常   当前的网络请求。请查看堆栈跟踪了解更多信息   有关错误的信息以及它在代码中的起源。

     

异常详细信息:System.NullReferenceException:不是对象引用   设置为对象的实例。

来源错误:

CustomerReturnItem customerreturnitem = new CustomerReturnItem();
customerreturnitem = List1[count];
customerreturnitem.CustomerReturnId = CustomerReturnId;
customerreturnitem.ItemId = PassClass.ItemId[count];"

这是我的帖子:

    HttpPost]
            public ActionResult Action4( List<CustomerReturnItem> List1)
            {

                CustomerReturn customerreturn = new CustomerReturn();
                UpdateModel(customerreturn);
                customerreturn.TransactionId = PassClass.TransactionId;
                customerreturn.CustomerId = PassClass.CustomerId;
                customerreturn.DateOfCustomerReturn = System.DateTime.Now;

                db.SaveChanges();
                int CustomerReturnId = customerreturn.CustomerReturnId;
                if (ModelState.IsValid)
                {
                    for (int count = 0; count < PassClass.ItemCount; count++)
                    {
                        CustomerReturnItem customerreturnitem = new CustomerReturnItem();

                        customerreturnitem = List1[count];
                        customerreturnitem.CustomerReturnId = CustomerReturnId;
                        customerreturnitem.ItemId = PassClass.ItemId[count];
                        UpdateModel(customerreturnitem);
                        db.SaveChanges();


                    }
                }

                return RedirectToAction("Action5");
            }

我的观点

@model BBTprogram.Models.CustomerReturnItem

@{
    ViewBag.Title = "Action4";
}

<h2>Action4</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)



        <table>
        <tr>
        <th>
        Item
        </th>
        <th>
        Quantity Baught
        </th>       

        <th>
         <div class="editor-label">
            @Html.LabelFor(model => model.CustomerReturnQuantity)
        </div>
        </th>
        <th>
        <div class="editor-label">
            @Html.LabelFor(model => model.CustomerReasonForReturn)
        </div>
        </th>
        <th>
         <div class="editor-label">
            @Html.LabelFor(model => model.Resellable)
        </div>
        </th>
         </tr>
         @for (int i = 0; i < ViewBag.CountInfo; i++)
         {

        <tr>
        <td>
        <div>
        @ViewBag.ItemInfo[i]
        </div>
        </td>
        <td>
        <div>
        @ViewBag.QuantityInfo[i]
        </div>
        </td>

        <td>

        <div class="editor-field">
            @Html.EditorFor(model => model.CustomerReturnQuantity)
            @Html.ValidationMessageFor(model => model.CustomerReturnQuantity)
        </div>
        </td>
        <td>

        <div class="editor-field">
            @Html.EditorFor(model => model.CustomerReasonForReturn)
            @Html.ValidationMessageFor(model => model.CustomerReasonForReturn)
        </div>
        </td>
        <td>

        <div class="editor-field">
            @Html.EditorFor(model => model.Resellable)
            @Html.ValidationMessageFor(model => model.Resellable)
        </div>
        </td>
        </tr>
         }
        </table>
        <p>
            <input type="submit" value="Create" />
        </p>

}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

这是我的模特:

namespace BBTprogram.Models
{
    public class CustomerReturnItem
    {
        //Primary Key
        [Key]
        [ScaffoldColumn(false)]
        [Required]
        public int CustomerReturnItemId { get; set; }
        // Foreign Key
        public int CustomerReturnId { get; set; }
        public int ItemId { get; set; }
        //Other
        [DisplayName("Quantity of returned items")]
        [Required(ErrorMessage = "Returned quantity of item is required")]
        [Range(1, 200,
        ErrorMessage = "Returned quantity of item must be between 1 and 200")]
        public int CustomerReturnQuantity { get; set; }

        [DisplayName("Reason for returns")]
        public string CustomerReasonForReturn { get; set; }

        [DisplayName("Resellable?")]
        [Required(ErrorMessage = "Resellable is required")]
        public bool Resellable { get; set; }


    }
}

1 个答案:

答案 0 :(得分:0)

您的视图似乎只呈现一个CustomerReturnItem

@model BBTprogram.Models.CustomerReturnItem

不是复数:List<BBTprogram.Models.CustomerReturnItem>

默认情况下Html.BeginForm发送给ActionResult当前模型。在您的情况下CustomerReturnItem。但是你的ActionResult期望List<CustomerReturnItem>

尝试按如下方式更改它:

[HttpPost]
public ActionResult Action4(CustomerReturnItem model)
            {
//**Your code here

}