title: Reverse sort with MVC3 Ajax WebGrid
body: <p>I want to apply sorting over Ajax, I managed to send the sort column name to my Controller and perform the orderby query in my database, can anyone tell me how can I send the sort order too?</p>
  
  <p>View</p>
  
  <pre><code>&lt;div id="grid"&gt;
  @{
      var grid = new WebGrid(canPage: true, rowsPerPage: Ctrl.PageSize, canSort: true, ajaxUpdateContainerId: "grid");
      grid.Bind(Model.Events, rowCount: Model.TotalRecords, autoSortAndPage: false);
  
      grid.Pager(WebGridPagerModes.All);
      @grid.GetHtml(htmlAttributes: new { id="grid" },
              columns: grid.Columns(
              grid.Column("Date"),
              grid.Column("Type"),
              grid.Column("Message")
  }
  &lt;/div&gt;
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>public ActionResult Index(int? page, string sort)
  {
      var events = GetCollection();
      var startPage = 0;
      var result = new EventListModel();
      if (page.HasValue &amp;&amp; page.Value &gt; 0)
      {
          startPage = page.Value - 1;
      }
  
      result.TotalRecords = events.Count();
  
      switch (sort)
      {
          case "Type":
              result.Events = events.FindAll().OrderBy(p =&gt; p.Type).Skip(startPage * PageSize).Take(PageSize).ToList();
              break;
          case "Message":
              result.Events = events.FindAll().OrderBy(p =&gt; p.Message).Skip(startPage * PageSize).Take(PageSize).ToList();
              break;
          case "Date":
              result.Events = events.FindAll().OrderBy(p =&gt; p.Date).Skip(startPage * PageSize).Take(PageSize).ToList();
              break;
      }
      return View(result);
  }
  </code></pre>