c# - 当我运行我的页面时,它返回一个 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
。我试过了try
,catch
它仍然返回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>
}
解决方案
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
推荐阅读
- android - 了解 Android 日志垃圾收集器
- image - Matlab中的稻谷分割
- javascript - 使用 splice react native 从数组中删除对象元素
- webpack - 如何捆绑将在“new Function()”中调用的脚本
- python - 创建新中间件后,无法使用 JWT 令牌在 Django Rest Framework 自定义请求中间件中使用用户
- ios - 如何在 iOS 中使用 Sqllite 验证登录表单进入下一页?
- swift - SwiftUI:在 ObservableObject 模型视图中使用函数初始化数据时出错
- clojure - 有没有一种方法可以重用 EDN 中已定义键的值?
- javascript - JSON 会发展到原生支持 Ecmascript 地图对象吗?
- regex - 提取数字并将其附加到不同的单元格中?