I have an Index (List) View in MVC5, that is populated from a model (Table ICS_Supplies).
I have added a textbox to add search filter for the users, on field ItemDescription (varchar). This works perfectly fine as follows:
View
<form asp-controller="Movies" asp-action="Index">
<p>
Search Supplies: <input type="text" name="SearchString">
<input type="submit" value="Filter" />
</p>
</form>
Controller
public ActionResult Index(string searchString, string SType, int? page, string YourRadioButton)
{
// Add SearchBox Filter
var catalogs = supplies.Where(s => s.ItemDescription.Contains(searchString ?? string.Empty));
// Add paging to the search results
var pageNumber = page ?? 1;
return View(catalogs.ToPagedList(pageNumber, 10));
}
This works perfectly. If the searchString is null, it brings back ALL results. IF the searchSring has a value, it brings back any results where ItemDescription Cotain the searchString Value.
I am trying to add a radiobutton to the index view so that the user can also filter on the field InvType, which is a char(1) field. It can be F (for Forms) or S (for supplies). So, I set the value of YourRadioButton to F or S depending on which is selected. . . as follows (with new code)
Index
<form asp-controller="Movies" asp-action="Index">
<div>
Supplies: @Html.RadioButton("YourRadioButton", "S")
Forms: @Html.RadioButton("YourRadioButton", "F")
</div>
<p>
Search Supplies: <input type="text" name="SearchString">
<input type="submit" value="Filter" />
</p>
</form>
And I update the Controller with additional code, as follows:
public ActionResult Index(string searchString, string SType, int? page, string YourRadioButton)
{
var supplies = db.ICS_Supplies.OrderBy(g => g.ItemDescription).ToList();
//var supplies2 = supplies.Where(s => s.InvType.Equals(mychoice));
var supplies2 = supplies.Where(s => s.InvType.Contains(YourRadioButton ?? string.Empty));
// Add SearchBox Filter
var catalogs = supplies2.Where(s => s.ItemDescription.Contains(searchString ?? string.Empty));
// Add paging to the search results
var pageNumber = page ?? 1;
return View(supplies2.ToPagedList(pageNumber, 10));
}
Now, I receive The following error
System.NullReferenceException
And it is referring to the following line of code (which I added)
var supplies2 = supplies.Where(s => s.InvType.Contains(YourRadioButton ?? string.Empty));
My question(s) are . . . Why does that kick out a NullReferenceException, but the other line works perfectly fine if it is null? And how do I resolve the issue - or is there a better way to add this second filter to my code?
This line works fine, null or not. They are both identical in how they are written, other than the Value of YourRadioButton is used, instead of searchString, and I am using InvType field instead of ItemDescription.
var catalogs = supplies2.Where(s => s.ItemDescription.Contains(searchString ?? string.Empty));
Keep in mind that I am VERY new to both MVC5 and C#, and so explaining why would help me a great deal to progress.
There does not seem to be a lot of information out there, in regards to using radio buttons in MVC5 . . . a rather simple concept in old Asp.net forms.