title: ASP.NET MVC3: WebGrid + Ajax Filters + Ajax Sorting & Paging
body: <p>Basically, I'm using WebGrid and I need to filter the results. The first problem I have here is it's my first time using WebGrid and I was hoping some of you could give me a hand with it... So far, I've managed to sort grid results and filter them with Ajax, but, when re-sorting the filtered results, the subset is lost and I go back to the beginning with the full set of results. I know why it's happening of course, but I don't figure out how to make it work.</p>
  
  <p>Example:</p>
  
  <p>On my view:</p>
  
  <pre><code>@model IQueryable&lt;Cities&gt;
  @section MoreScripts
  {
      &lt;script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  }
  
  @using (Ajax.BeginForm(new AjaxOptions { InsertionMode = InsertionMode.Replace,     UpdateTargetId = "GridData"}))
  {
      &lt;fieldset&gt;
          &lt;legend&gt;Search Filters&lt;/legend&gt;
          &lt;br /&gt;
          &lt;div&gt;
              Name
          &lt;/div&gt;
          &lt;div&gt;
              @Html.TextBox("Name")
          &lt;/div&gt;
          &lt;p&gt;
              &lt;input type="submit" value="Search" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  
  &lt;div id="GridData"&gt;
      @Html.Partial("Grid", Model)
  &lt;/div&gt;
  </code></pre>
  
  <p>My Partial View: </p>
  
  <pre><code>@model IQueryable&lt;Cities&gt;
  
  @{
      var grid = new WebGrid&lt;Cities&gt;(null,rowsPerPage: 5, defaultSort: "Nombre", ajaxUpdateContainerId: "GridData");
      grid.Bind(Model, autoSortAndPage: true, rowCount: Model.Count());
      @grid.GetHtml(columns: 
                      grid.Columns(
                                  grid.Column("Name", "Name", canSort: true),
                                  grid.Column("CreationDate", "Creation Date", canSort: true),
                                  grid.Column("Active", "Active", canSort: true, format: @&lt;text&gt;&lt;input type="checkbox" disabled="disabled" value="@item.ID" @(item.Active == true ? "Checked" : null) /&gt;&lt;/text&gt;),
                                                                              grid.Column(format: (item) =&gt; Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "editLink smallCell", @title = "Edit" })),
                                  grid.Column(format: (item) =&gt; Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { @class = "deleteLink smallCell", @title = "Delete" }))),
                            tableStyle: "webgrid",
                            headerStyle: "webgrid-header",
                            footerStyle: "webgrid-footer",
                            alternatingRowStyle: "webgrid-alternating-row",
                            selectedRowStyle: "webgrid-selected-row",
                            rowStyle: "webgrid-row-style");    
  }
  </code></pre>
  
  <p>And finally what am doing wrong is here, on my controller:</p>
  
  <pre><code>    public ActionResult Index()
      {
          return View(repository.GetAllRecords().OrderByDescending(f =&gt; f.CreationDate));
      }
  
      [HttpPost]
      public ActionResult Index(string name)
      {
          var data = repository.GetAllRecords();
  
          if(!string.IsNullOrEmpty(name))
              data = data.Where(a =&gt; a.Name.Contains(name));
  
          data = data.OrderByDescending(f =&gt; f.CreationDate);
  
          return PartialView("Grid", data);
      }
  </code></pre>
  
  <p>I'm also using a class WebGrid : WebGrid as seen here:
  <a href="http://archive.msdn.microsoft.com/mag201107WebGrid/Release/ProjectReleases.aspx?ReleaseId=5667" rel="nofollow">http://archive.msdn.microsoft.com/mag201107WebGrid/Release/ProjectReleases.aspx?ReleaseId=5667</a></p>
  
  <p>So, this actually works fine for filtering, but once you get the filtered results and then try to change the sort order of your narrowed search results, you lose the elements and the 'name' parameter's value because the WebGrid goes agains the first controller action. Probably this is not the best approach, but as I said, I've never used WebGrid so I'm willing to learn. Any help would be really appreciated. Thanks.</p>