首页 > 解决方案 > 当我运行我的页面时,它返回一个 null 错误,就好像它没有捕获传入的信息一样

问题描述

我正在创建一个名为垃圾收集器的项目,来自 Employee Controller,他们将能够确认垃圾收集已完成,并将向客户添加费用。

我在 Employee Controller 中创建了以下逻辑,并在Index View中创建了一个按钮来捕获确认的拾取现在是真的并添加费用金额。它正在捕获费用金额,但无法找到我的客户。

我收到一个错误:

“你调用的对象是空的”

客户是null。我不确定为什么它显示为,null因为在索引逻辑中我设置它以在员工的邮政编码中找到客户并且它恢复正常,但是对于 ConfirmPickup 逻辑客户返回为null,它也显示我的编辑actionlink和详细信息也出现了同样的错误actionlink

员工控制器

以下方法有效并且正在带回客户:

 public ActionResult Index(string searchBy, string search)
        {
            var UserId = User.Identity.GetUserId();
            Employee employee = db.Employees.Where(e => e.ApplicationId == UserId).FirstOrDefault();
            var filteredCustomer = db.Customers.Where(c => c.ZipCode == employee.ZipCode && c.PickupDay == DateTime.Today.DayOfWeek.ToString() && c.ConfirmPickedup == false);


            if (searchBy == "Pickup Day")
            {
                return View(db.Customers.Where(c => c.PickupDay == search && c.ZipCode == employee.ZipCode).ToList());
            }

            return View(filteredCustomer);
        }

下面的方法不起作用:

PickupConfirmed(Customer customer)是回报null。我试过了trycatch它仍然返回null

 public ActionResult PickupConfirmed(Customer customer)
        {
            //var UserId = User.Identity.GetUserId();
            var pickupConfirmedCustomer = db.Customers.Find(customer.Id);
            pickupConfirmedCustomer.AccountBalance += 19.99;
            pickupConfirmedCustomer.ConfirmPickedup = true;
            db.SaveChanges();
            return RedirectToAction("Index");

        }

这是我的索引视图:

@model IEnumerable<Trash_Collector.project.Models.Customer>

@{
    ViewBag.Title = "Index";
}

<h2>Pickups For Today</h2>

<p>
    @using (Html.BeginForm("Index", "Employees", FormMethod.Get))
    {
        <b>Search By:</b> @Html.RadioButton("searchBy", "Pickup Day", true) <text>Pickup Day</text>
        @Html.TextBox("search") <input type="submit" value="Search" />
    }
</p>

@using (Html.BeginForm("PickupConfirmed", "Employees", FormMethod.Get))
{
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.StreetAddress)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.City)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.State)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ZipCode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PickupDay)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.FirstName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.LastName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.StreetAddress)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.City)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.State)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ZipCode)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PickupDay)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                    @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                </td>
                <td>
                    <input type="submit" value="Confirm Pickup" />
                </td>
            </tr>
        }

    </table>
}

标签: c#htmlasp.net-mvc

解决方案


Your code
var filteredCustomer = db.Customers.Where(c => c.ZipCode == employee.ZipCode && c.PickupDay == DateTime.Today.DayOfWeek.ToString() && c.ConfirmPickedup == false);

change code
var filteredCustomer = db.Customers.Where(c => c.ZipCode == employee.ZipCode && c.ConfirmPickedup == false);


Maybe this portion is a problem 
 c.PickupDay == DateTime.Today.DayOfWeek.ToString() //is not equal

推荐阅读