Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MVC5:如何根据所选下拉项进行过滤_Javascript_C#_Asp.net Mvc 5 - Fatal编程技术网

Javascript MVC5:如何根据所选下拉项进行过滤

Javascript MVC5:如何根据所选下拉项进行过滤,javascript,c#,asp.net-mvc-5,Javascript,C#,Asp.net Mvc 5,这个问题被标记为另一篇文章的副本。这篇文章详细而具体地讨论了“NullReference”异常。它提供了大量专门针对空引用错误的可能原因的信息 我的问题不是针对空引用错误的。我的问题是“如何根据选定的下拉框项筛选结果”。当然,在我的试错过程中,我遇到了一个在这里提出的空引用错误。但这不是我问题的基础。我试图做一个特定的功能,其他人可能也会觉得有用。(1) 也许有更好的方法来实现我的目标——也许有人会分享这一点。(2) 我们可能会解决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();

    }
    public PartialViewResult OrderHistoryPartial(int id)
    {
        return PartialView(
            db.ICS_Order_History.Where(r => r.DeliveryUnitID == id).OrderByDescending(r => r.OrderDate)
                .ToList());
    }
索引视图

@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>
我认为我已经非常接近于解决这个问题,但我已经在不寻求帮助的情况下尽我所能地解决了这个问题

目前,我发现以下错误:

对象引用未设置为对象的实例。说明: 在执行当前操作期间发生未处理的异常 网络请求。有关详细信息,请查看堆栈跟踪 错误及其在代码中的起源

异常详细信息:System.NullReferenceException:对象引用 未设置为对象的实例

源错误:

第43行:第44行:第45行:@foreach(模型中的变量项) 第46行:{第47行:


今天早些时候我问了这个问题,但我删除了那篇文章,因为我认为这篇文章更有条理,并且提供了更详细的信息。

您正在将部分视图ViewData.Model传递到您的部分视图中。该模型为空,因为您调用的视图没有参数。两种可能的修复方法是

  • 将代码添加到OrderHistoryPartial.OrderHistoryPartial中的IndexController.Index,并使用view(结果)返回模型
  • 改为使用HTML.Action调用部分视图,并传递ID@HTML.Action(“OrderHistoryPartial”,“OrderHistoryPartial”,new{ID=})

  • 您的
    Index()
    方法需要是
    返回视图(新列表);
    -您没有返回模型,因此
    @foreach(模型中的var项)
    抛出一个
    NullReferenceException
    “我的问题不特定于NullReference错误”。然后解决你的问题,因为你的问题仍然存在。这阻碍了你的进步,阻碍了任何人帮助你。
    <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>
    
        public PartialViewResult OrderHistoryPartial(int id)
        {
            return PartialView(
                db.ICS_Order_History.Where(r => r.DeliveryUnitID == id).OrderByDescending(r => r.OrderDate)
                    .ToList());
        }