-1

This Question was marked as a duplicate to another post. That post addresses the "NullReference" Exception specifically and in great detail. It provides a lot of information specifically to and exclusively to the possible causes of NullReference Errors.

My Question is NOT specific to NullReference Errors. My question is "HOW TO FILTER RESULTS BASED ON SELECTED DROPDOWN BOX ITEMS". Certainly, along my course of trial and error - I encountered a NullReference error which was brought up here. But that's not the basis of my question. I am trying to do a specific function, that others may also find useful. (1) There may be better ways to accomplish my goal - and perhaps someone would share that. (2) we may resolve the NullReference error, only to find more errors that pop up. And therefore, this post is non specific to the NullReference Error, and specific to accomplishing the job of Filtering Results based on selected dropdown item. NullReference was just one of the errors along the way, in trying to accomplish the goal of the true question. So, I don't see this as a duplicate. But, it's fine.

How do I filter results, based on what is selected from a dropdown box? I am using MVC5, C# and javascript.

I found (what I think is) a good example online, and I modified it for my own use to test. Here is what I have so far

Index Controller:

public ViewResult Index()


    {
        var countries = new SelectList(
             db.ICS_Units.Select(r => r.DeliveryUnit).Distinct().ToList());

        ViewBag.Countries = countries;
        return View();

    }

Index 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 added to Index View:

@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>

Partial View (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 Controller:

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

I think that I am VERY close to resolving this issue, but I have taken it as far as I can without asking for some help.

Currently, I get the following error:

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 43: Line 44: Line 45: @foreach (var item in Model) Line 46: { Line 47:

I asked this question earlier today, but I have deleted that post because I think this is a bit more organized and provides more detailed information.

ExecChef
  • 387
  • 2
  • 13
  • Your `Index()` method needs to be `return View(new List);` - You are not returning a model so `@foreach (var item in Model) ` throws a `NullReferenceException`. –  Nov 19 '18 at 21:45
  • “My Question is NOT specific to NullReference Errors”. Then fix your question, since your question still has that problem. This is blocking your progress and blocking anyone from helping you. – Dour High Arch Nov 20 '18 at 18:14

1 Answers1

0

You are passing into your partial view ViewData.Model which is null because you are calling the view with no parameter. The 2 possible fixes are

  1. Add the code to the IndexController.Index from OrderHistoryPartial.OrderHistoryPartial and return the model using view(result)
    1. Call your Partial View with HTML.Action instead and pass an ID @html.Action("OrderHistoryPartial","OrderHistoryPartial",new { id = })
Breian Wells
  • 111
  • 3