vendredi 17 juin 2016

Model is null after postback in pager

On my page I have:

  • a 'filter' section - a couple of checkboxes and textboxes,
  • a 'search' button,
  • grid with paging where the results are displayed. The grid is from Telerik (, but I don't think this matters.

Searching works OK - I can input some text or check a checkbox in the filter section and the appropriate results are displayed.

Paging works OK only if I use it when the page is loaded (that means before I click Search button, in this case the url is '...Home').

But if click search first (in this case the url will become '...Home/Search') and then try to go to another page on the grid then I get an exception in the Search method, because the model.Filter parameter is null (System.NullReferenceException: Object reference not set to an instance of an object.)

I tried to solve the problem in many different ways (with RedirectToAction method, storing the filter to session and use it in Search method,...) but no solution worked in all scenarios. Any ideas?

My simplified code:


public ActionResult Index()
    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; }


@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;" })
    <!-- a bunch of other checkboxes -->
    <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" })
    <!-- a bunch of other textboxes -->
    <button class="k-button" id="button-refresh" style="margin: 10px 0 0 135px;">Refresh</button>
    <hr />
        .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);
        .Pageable(p => p
            .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); }))

Aucun commentaire:

Enregistrer un commentaire