title: Changing number of table elements displayed on page resets my search results
body: <p>I have numerous data displayed in a table, let's say a long list of users (first name &amp; last name), so I set up a paging feature to display the elements by pages via the PagedList NuGet package. I was inspired by this tutorial: <a href="https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application</a></p>
  
  <p>I implemented a drop-down list in my view, so that I can directly <strong>choose the number of elements to display</strong> per page. I managed to include a jQuery script that makes the page size update whenever the drop-down list has a new selected value.</p>
  
  <p><a href="https://i.stack.imgur.com/103FX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/103FX.png" alt="DropDownList allowing to set the number of elements to display per page"></a></p>
  
  <p>Using the mentioned tutorial , I also added a search feature: submitting a string in a <strong>search form</strong> allows to filter the data.</p>
  
  <p><a href="https://i.stack.imgur.com/iIPrj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iIPrj.png" alt="Search form"></a></p>
  
  <p>My problem is: changing the page size by selecting a new value in the drop-down list <strong>after</strong> having done a search doesn't work: the search results are reset, all the entries being displayed instead. I guess I forgot to pass some parameter somewhere but I just can't figure out where...</p>
  
  <p>Here is my controller:</p>
  
  <pre><code>public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page, int? PageSize)
  
      // Sort order is passed to view in order to keep it intact while clicking in another page link
      ViewBag.CurrentSort = sortOrder;
  
      // Ascending or descending sorting by first or last name according to sortOrder value
      ViewBag.LastNameSortParm = String.IsNullOrEmpty(sortOrder) ? "lastname_desc" : "";
      ViewBag.FirstNameSortParm = sortOrder == "firstname" ? "firstname_desc" : "firstname";
  
      // Not sure here
      if (searchString == null)
      {
          searchString = currentFilter;
      }
  
      // Pass filtering string to view in order to maintain filtering when paging
      ViewBag.CurrentFilter = searchString;
  
      var users = from u in _db.USER select u;
  
      // FILTERING
      if (!String.IsNullOrEmpty(searchString))
      {
          users = users.Where(u =&gt; u.lastname.Contains(searchString)
                                || u.firstname.Contains(searchString)
      }
  
      // Ascending or descending filtering by first/last name
      switch (sortOrder)
      {
      case "lastname": // Ascending last name
          users = users.OrderBy(u =&gt; u.lastname);
          break;
      case "lastname_desc": // Descending last name
          users = users.OrderByDescending(u =&gt; u.lastname);
          break;
      case "firstname": // Ascending first name
          users = users.OrderBy(u =&gt; u.firstname);
          break;
      case "firstname_desc": // Descending first name
          users = users.OrderByDescending(u =&gt; u.firstname);
          break;
      default:
          users = users.OrderBy(u =&gt; u.lastname);
          break;
  }
  
  
  // DROPDOWNLIST FOR UPDATING PAGE SIZE
  int count = _db.USER.OrderBy(e =&gt; e.Id).Count(); // Total number of elements
  
  // Populate DropDownList
  ViewBag.PageSize = new List&lt;SelectListItem&gt;() {
      new SelectListItem { Text = "10", Value = "10", Selected = true },
      new SelectListItem { Text = "25", Value = "25" },
      new SelectListItem { Text = "50", Value = "50" },
      new SelectListItem { Text = "100", Value = "100" },
      new SelectListItem { Text = "All", Value = count.ToString() }
  };
  
  int pageNumber = (page ?? 1);
  int pageSize = (PageSize ?? 10);
  ViewBag.psize = pageSize;
  
  return View(users.ToPagedList(pageNumber, pageSize));
  }
  </code></pre>
  
  <p>And my Index.cshtml view:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;script src="~/Scripts/jquery-3.2.1.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $(function () { // Submit pageSizeForm when another pageSize value is selected
      $("#pageSize").change(function () {
          $("#pageSizeForm").submit();
      });
  });
  &lt;/script&gt;
  
  @model PagedList.IPagedList&lt;AfpaSIPAdmin.Models.USER&gt;
  @using PagedList.Mvc;
  &lt;link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" /&gt;
  
  @{
      ViewBag.Title = "Users management";
  }
  
  &lt;h1&gt;Users management&lt;/h1&gt;
  
  &lt;!-- Creating a new entry in table --&gt;
  &lt;p&gt;
      @Html.ActionLink("Create new user", "Create")
  &lt;/p&gt;
  
  
  &lt;!-- Filtering table entries --&gt;
  @using (Html.BeginForm("Index", "Users", FormMethod.Get, new { id = "filterForm" }))
  {
      &lt;p&gt;
          Filter: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string, new { @placeholder = "First or last name..." })
          &lt;input type="submit" value="Apply"/&gt;
      &lt;/p&gt;
  }
  
  &lt;!-- Display table --&gt;
  &lt;table class="table"&gt;
      &lt;tr&gt;
          &lt;th&gt;
              @Html.ActionLink("Last name", "Index", new {
                  sortOrder = ViewBag.LastNameSortParm,
                  currentFilter = ViewBag.CurrentFilter
              })
          &lt;/th&gt;
          &lt;th&gt;
              @Html.ActionLink("First name", "Index", new {
                  sortOrder = ViewBag.FirstNameSortParm,
                  currentFilter = ViewBag.CurrentFilter
              })
          &lt;/th&gt;
          &lt;th style="min-width: 170px"&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
      @foreach (var item in Model)
      {
          &lt;tr&gt;
             &lt;td style = "min-width: 150px"&gt;
                  @Html.DisplayFor(modelItem =&gt; item.lastname)
              &lt;/td&gt;
              &lt;td style = "min-width: 150px"&gt;
                  @Html.DisplayFor(modelItem =&gt; item.firstname)
              &lt;/td&gt;
  
              &lt;td&gt; &lt;!-- Using images as buttons for actions --&gt;
                  &lt;a href="@Url.Action("Edit", "Users", new { id = item.Id })" title="Edit"&gt;
                      &lt;img src="~/Content/images/edit.gif" /&gt;
                  &lt;/a&gt;
                  &lt;a href="@Url.Action("Details", "Users", new { id = item.Id })" title="Details"&gt;
                      &lt;img src="~/Content/images/info.gif" /&gt;
                  &lt;/a&gt;
                  &lt;a href="@Url.Action("Delete", "Users", new { id = item.Id })" title="Delete"&gt;
                      &lt;img src="~/Content/images/delete.gif" /&gt;
                  &lt;/a&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  &lt;/table&gt;
  
  &lt;br/&gt;
  
  &lt;!-- Paging --&gt;
  @using (Html.BeginForm("Index", "Users", FormMethod.Get, new { id = "pageSizeForm" }))
  {
      &lt;div class="pager"&gt;
          Page @(Model.PageCount &lt; Model.PageNumber ? 0 : Model.PageNumber) sur @Model.PageCount&lt;br/&gt;
  
          @Model.Count of @Model.TotalItemCount elements 
  
          @Html.PagedListPager(Model, page =&gt; Url.Action("Index", new {
              page,
              sortOrder = ViewBag.CurrentSort,
              currentFilter = ViewBag.CurrentFilter,
              searchString = ViewBag.CurrentFilter,
              pageSize = ViewBag.psize
          }))
  
          &lt;!-- DropDownList for setting page size --&gt;
          Elements per page :
          @Html.DropDownList("pageSize")
      &lt;/div&gt;
  }
  </code></pre>