C# 当我运行我的页面时,它返回一个null错误,就好像它没有捕获传入的信息一样
我正在创建一个名为“垃圾收集器”的项目,由员工C# 当我运行我的页面时,它返回一个null错误,就好像它没有捕获传入的信息一样,c#,html,asp.net-mvc,C#,Html,Asp.net Mvc,我正在创建一个名为“垃圾收集器”的项目,由员工控制器,他们将能够确认垃圾收集已完成,并向客户添加费用金额 我在员工控制器中创建了下面的逻辑,并在索引中创建了一个按钮视图,以捕获已确认的取货现在为真并添加费用金额。它正在捕获费用金额,但无法找到我的客户 我收到一个错误: “对象引用未设置为对象的实例” 客户为空。我不确定它为什么显示为null,因为在索引逻辑中,我设置它是为了在员工的Zipcode中查找客户,结果很好,但在确认拾取逻辑中,客户返回为null,它还为我的编辑actionlink和详细
控制器,他们将能够确认垃圾收集已完成,并向客户添加费用金额
我在员工控制器中创建了下面的逻辑,并在索引中创建了一个按钮视图
,以捕获已确认的取货现在为真并添加费用金额。它正在捕获费用金额,但无法找到我的客户
我收到一个错误:
“对象引用未设置为对象的实例”
客户为空
。我不确定它为什么显示为null
,因为在索引逻辑中,我设置它是为了在员工的Zipcode中查找客户,结果很好,但在确认拾取逻辑中,客户返回为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);
}
下面的方法不起作用:
@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>
}
pickupconfixed(客户)
返回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>
}
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
今天的接送
@使用(Html.BeginForm(“Index”,“Employees”,FormMethod.Get))
{
搜索人:@Html.RadioButton(“搜索人”,“取货日”,true)取货日
@文本框(“搜索”)
}
@使用(Html.BeginForm(“PickupConfirmed”、“Employees”、FormMethod.Get))
{
@DisplayNameFor(model=>model.FirstName)
@DisplayNameFor(model=>model.LastName)
@DisplayNameFor(model=>model.StreetAddress)
@DisplayNameFor(model=>model.City)
@DisplayNameFor(model=>model.State)
@DisplayNameFor(model=>model.ZipCode)
@DisplayNameFor(model=>model.PickupDay)
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.FirstName)
@DisplayFor(modelItem=>item.LastName)
@DisplayFor(modelItem=>item.StreetAddress)
@DisplayFor(modeleItem=>item.City)
@DisplayFor(modelItem=>item.State)
@DisplayFor(modelItem=>item.ZipCode)
@DisplayFor(modelItem=>item.PickupDay)
@ActionLink(“编辑”,“编辑”,新的{id=item.id})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
}
}
如何将您的客户
传递给PickupConfirmed
操作?您可以向我们展示您的前端(JavaScript/web表单)吗?您可以通过手动SQL查询确认表Customer
中的客户和Customer.Id
中的客户作为查询值实际存在吗?我添加了HTML索引视图,因此可能我没有将我的客户传递到PickupConfirms中?我的前端看起来像这样,这里是链接,一旦我点击确认按钮,它将转到ActionResult PickupConfirm,我假设它在点击按钮时已经捕获了客户信息。但当我调试它时,如果我手动键入Id号,它会将金额添加到客户。是的,它确实存在,因为当我调试它并手动键入客户的Id号时,金额会添加到客户。