On my page, I have:
Job Search OK - I can enter text or check the box in the filter section and display the corresponding results.
Paging works fine only if I use it when loading a page (this means that before I click the "Search" button, in this case the URL is "... Home").
But if you first click "Search" (in this case, the URL will become "... Home / Search"), and then try to go to another page in the grid, then I get an exception in the search method, because the model. The filter parameter is null (System.NullReferenceException: the reference to the object is not installed in the instance of the object.)
I tried to solve the problem in different ways (using the RedirectToAction method, storing the filter for the session and using it in the search method ...), but not a single solution worked in all scenarios. Any ideas?
My simplified code:
HomeController:
public ActionResult Index() { // On page load display all data without filters. var filter = new OverviewFilterModel { Type1 = true, Type2 = true, WorkingOrder = "" }; ViewBag.Results = GetResults(filter); return View(new HomeModel { Filter = filter }); } public ActionResult Search(HomeModel model) { ViewBag.Results = GetResults(model.Filter); return View("Index"); } public class OverviewFilterModel { public bool Type1 { get; set; } public bool Type2 { get; set; } public string WorkingOrder { get; set; } } public class HomeModel { public OverviewFilterModel Filter { get; set; } public IEnumerable<OverviewResultsModel> Results { get; set; } }
View:
@model HomeModel @using (Html.BeginForm("Search", "Home", FormMethod.Post, new { @class = "form-inline" })) { <div class="form-group" style="margin-left: 135px;"> @Html.CheckBoxFor(p => p.Filter.Type1)@Html.LabelFor(p => p.Filter.Type1, new { style = "margin: 0 15px 0 5px;" }) </div> <br /> <div class="form-group"> <label style="width: 130px; text-align: right;">Delovni nalog</label> @Html.TextBoxFor(p => p.Filter.WorkingOrder, new { @class = "form-control ecert-filter-small", @autocomplete = "off" }) </div> <button class="k-button" id="button-refresh" style="margin: 10px 0 0 135px;">Refresh</button> <hr /> @(Html.Kendo().Grid<OverviewResultsModel>() .BindTo((IEnumerable<OverviewResultsModel>)ViewBag.Results) .Name("gridOverview") .Events(p => p.Change("overviewOnRowSelect")) .Columns(columns => { columns.Template(@<text>@Html.ActionLink("WorkingOrder", "Index", "WO", new { dn = @item.WorkingOrder }, new { @class = "selectable-dn" })</text>).Title(""); columns.Bound(p => p.Type); columns.Bound(p => p.WorkingOrder); columns.Bound(p => p.Date); columns.Bound(p => p.ProductId); columns.Bound(p => p.ProductName); }) .Selectable() .Pageable(p => p .Refresh(true) .PageSizes(true) .ButtonCount(10) .Messages(q => { q.Display("{0} - {1} od {2} records"); q.Empty("No data for selected filter"); q.ItemsPerPage("Number of records per page"); }) ) .DataSource(p => p.Server().PageSize(20).Model(q => { q.Id(r => r.WorkingOrder); })) ) }
source share