title: Mvc Asp Sorting items
body: <p>I've used PagedList.MVC for pagination in my application. This is working fine. Pagination is working fine and also the OrderBy dropdownlist is working fine. When i select value from dropdownlist it gives the desired sorted result. I mean if i select A-Z then it sorts the items fine in ascending order but when i click 2nd page or next page it doesn't sort any item and the url is become like this </p>
  
  <p><a href="http://localhost:41213/Home/Products?page=2&amp;pageSize=6" rel="nofollow">http://localhost:41213/Home/Products?page=2&amp;pageSize=6</a></p>
  
  <p>And also how can I make this pageSize=6 a dynamic ? I want this to put in dropdownlist like {5,10,15,20} and when user selects a value then it should display that selected numbers of item.</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>    [HttpGet]
      public ActionResult Products(int? OrderBy, int page=1, int pageSize=6)
      {
          private Shopping db = new Shopping();
          switch (OrderBy)
          {
              case 1:
                  List&lt;Product&gt; listProductsasc = db.Products.OrderBy(p =&gt; p.Name).ToList();
                  PagedList&lt;Product&gt; modelasc = new PagedList&lt;Product&gt;(listProductsasc, page, pageSize);
                  return View(modelasc);
              case 2:
                  List&lt;Product&gt; listProductsdesc = db.Products.OrderByDescending(p =&gt; p.Name).ToList();
                  PagedList&lt;Product&gt; modeldesc = new PagedList&lt;Product&gt;(listProductsdesc, page, pageSize);
                  return View(modeldesc);
              default:
                  List&lt;Product&gt; listProducts = db.Products.ToList();
                  PagedList&lt;Product&gt; modeldefault = new PagedList&lt;Product&gt;(listProducts, page, pageSize);
                  return View(modeldefault);
          }
      }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model PagedList.IPagedList&lt;FypStore.Models.Product&gt;
  @using PagedList.Mvc
  &lt;div class="row"&gt;
          &lt;div class="col-sm-6"&gt;
              @using (Html.BeginForm("Products", "Home", FormMethod.Get, new { id = "Form1" }))
              {
                  &lt;div class="col-sm-8"&gt;
                      Sort by:
                      @Html.DropDownList("OrderBy", new List&lt;SelectListItem&gt;
  
                       {
                          new SelectListItem{ Text="A-Z", Value = "1" },
                          new SelectListItem{ Text="Z-A", Value = "2" }
                       }, "-- Order By --")
  
                  &lt;/div&gt;
                  &lt;div class="col-sm-4"&gt;
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-offset-2 col-md-10"&gt;
                              &lt;input type="submit" class="btn btn-default" value="Filter" /&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              }
          &lt;/div&gt;
  &lt;/div&gt;
  &lt;br /&gt;
  
  @Html.PagedListPager(Model, page =&gt; Url.Action("Products", new { page, pageSize = Model.PageSize }))
  Showing @Model.FirstItemOnPage to @Model.LastItemOnPage of @Model.TotalItemCount Products
  </code></pre>