1

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

“你调用的对象是空的。

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息: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; }


    }
}
4

1 回答 1

0

看来您的 View 只呈现一个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

}
于 2013-09-06T11:32:31.957 回答