BACKGROUND
I'm working in ASP.NET MVC 5. I have a list of products and want to filter them by name. I have a created a small form that sits above the product list.
CODE
Here is the Razor form
@using (Html.BeginForm("Page", "Inventory", routeValues: new { showDeleted = false }, method: FormMethod.Get))
{
<div class="row">
<div class="col-md-12">
<div class="form-group">
@Html.LabelFor(model => model.Search.SearchTerm, new { @class = "form-label" })
<div class="controls">
@Html.EditorFor(m => m.Search.SearchTerm, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Search.SearchTerm, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-primary pull-right filter">Filter Results</button>
</div>
</div>
}
And here is the controller method it is suppose to hit:
public ActionResult Page(SearchViewModel search, bool showDeleted, int page = 1)
{
var viewModel = _productService.GetPagedProducts(search, showDeleted, page);
return View(viewModel);
}
Submitting the filter with whatever search terms breaks the application and gives the following error:
The parameters dictionary contains a null entry for parameter 'showDeleted' of non-nullable type 'System.Boolean' for method 'System.Web.Mvc.ActionResult Page(Proj.ViewModels.Shared.SearchViewModel, Boolean, Int32)' in 'Proj.Controllers.InventoryController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
I know why it is failing, it is because the showDeleted
I've set up is getting ignored and thrown away! But I do not know how to fix this.