【发布时间】:2019-04-22 04:52:17
【问题描述】:
这个问题被标记为与另一个帖子重复。那篇文章特别详细地解决了“NullReference”异常。它提供了大量专门针对 NullReference 错误的可能原因的信息。
我的问题并非特定于 NullReference 错误。我的问题是“如何根据选定的下拉框项目过滤结果”。当然,在我的反复试验过程中 - 我遇到了这里提出的 NullReference 错误。但这不是我的问题的基础。我正在尝试做一个特定的功能,其他人可能也会觉得有用。 (1) 可能有更好的方法来实现我的目标——也许有人会分享这一点。 (2)我们可能会解决NullReference错误,只是发现更多的错误弹出。因此,这篇文章不是特定于 NullReference 错误,而是特定于根据选定的下拉项完成过滤结果的工作。 NullReference 只是尝试完成真正问题的目标的过程中的错误之一。所以,我不认为这是重复的。但是,没关系。
如何根据从下拉框中选择的内容过滤结果?我正在使用 MVC5、C# 和 javascript。
我在网上找到(我认为是)一个很好的例子,我修改了它以供我自己测试使用。这是我到目前为止所拥有的
索引控制器:
public ViewResult Index()
{
var countries = new SelectList(
db.ICS_Units.Select(r => r.DeliveryUnit).Distinct().ToList());
ViewBag.Countries = countries;
return View();
}
索引视图
@model IEnumerable<ICS20web.Models.ICS_Order_History>
@{
ViewBag.Title = "Index";
}
<fieldset>
<h2>Index</h2>
<div>
@Html.DropDownList("Countries", "select a Unit")
</div>
@{
Html.RenderPartial("OrderHistoryPartial", ViewData.Model);
}
<br />
<div id="target">
</div>
<div id="log">
</div>
</fieldset>
JavaScript 添加到索引视图:
@Scripts.Render("~/Scripts/jquery-1.10.2.min.js")
<script type="text/javascript">
$(document).ready(function () {
$("#testList").change(function () {
$("#log").ajaxError(function (event, jqxhr, settings, exception) {
alert(exception);
});
var countrySelected = $("select option:selected").first().text();
$.get('@Url.Action("OrderHistoryPartial")',
{ id: countrySelected }, function (data) {
$("#target").html(data);
});
});
});
</script>
部分视图(OrderHistoryPartial):
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.DeliveryUnitID)
</th>
<th>
@Html.DisplayNameFor(model => model.LoginID)
</th>
<th>
@Html.DisplayNameFor(model => model.SuppliesID)
</th>
<th>
@Html.DisplayNameFor(model => model.OrderDate)
</th>
<th>
@Html.DisplayNameFor(model => model.ExpectedMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.ExpectedYear)
</th>
<th>
@Html.DisplayNameFor(model => model.OrderedBy)
</th>
<th>
@Html.DisplayNameFor(model => model.OrderAmount)
</th>
<th>
@Html.DisplayNameFor(model => model.Measure)
</th>
<th>
@Html.DisplayNameFor(model => model.CurrentStatus)
</th>
<th>
@Html.DisplayNameFor(model => model.CurrentStatusDate)
</th>
<th>
@Html.DisplayNameFor(model => model.EmergencyOrder)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DeliveryUnitID)
</td>
<td>
@Html.DisplayFor(modelItem => item.LoginID)
</td>
<td>
@Html.DisplayFor(modelItem => item.SuppliesID)
</td>
<td>
@Html.DisplayFor(modelItem => item.OrderDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.ExpectedMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.ExpectedYear)
</td>
<td>
@Html.DisplayFor(modelItem => item.OrderedBy)
</td>
<td>
@Html.DisplayFor(modelItem => item.OrderAmount)
</td>
<td>
@Html.DisplayFor(modelItem => item.Measure)
</td>
<td>
@Html.DisplayFor(modelItem => item.CurrentStatus)
</td>
<td>
@Html.DisplayFor(modelItem => item.CurrentStatusDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmergencyOrder)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |
@Html.ActionLink("Details", "Details", new { id = item.OrderID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
</td>
</tr>
}
</table>
OrderHistoryPartial 控制器:
public PartialViewResult OrderHistoryPartial(int id)
{
return PartialView(
db.ICS_Order_History.Where(r => r.DeliveryUnitID == id).OrderByDescending(r => r.OrderDate)
.ToList());
}
我认为我已经非常接近解决这个问题了,但我已经尽可能地解决了这个问题,没有寻求任何帮助。
目前,我收到以下错误:
对象引用未设置为对象的实例。描述: 当前执行期间发生未处理的异常 网络请求。请查看堆栈跟踪以获取有关的更多信息 错误及其起源于代码的位置。
异常详细信息:System.NullReferenceException:对象引用 未设置为对象的实例。
来源错误:
第 43 行:第 44 行:第 45 行:@foreach(模型中的变量项) 第 46 行:{ 第 47 行:
我今天早些时候问过这个问题,但我已经删除了那个帖子,因为我认为这更有条理并且提供了更详细的信息。
【问题讨论】:
-
你的
Index()方法必须是return View(new List<ICS_Order_History>);- 你没有返回一个模型,所以@foreach (var item in Model)抛出一个NullReferenceException。 -
“我的问题并非特定于 NullReference 错误”。然后解决您的问题,因为您的问题仍然存在该问题。这会阻碍你的进步,阻碍任何人帮助你。
标签: javascript c# asp.net-mvc-5