绑定数组错误

本文关键字:错误 数组 绑定 | 更新日期: 2023-09-27 18:30:20

我目前在我的系统中有一个列表视图,其中显示了html输入列表和一个创建按钮。我想为每行输入创建一个买家退货商品。对不起,它太复杂了。我尝试了以下方法,但出现错误:

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

说明:执行 当前 Web 请求。请查看堆栈跟踪以获取更多信息 有关错误及其在代码中起源位置的信息。

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

源错误:

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; }

    }
}

绑定数组错误

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

@model BBTprogram.Models.CustomerReturnItem

非复数:List<BBTprogram.Models.CustomerReturnItem>

默认情况下,Html.BeginForm发送到当前模型ActionResult。在您的情况下CustomerReturnItem.但您的操作结果期望List<CustomerReturnItem>

尝试按如下方式更改它:

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