title: (C#) Unable to transfer order state using Session variable in ASP.NET MVC
body: <p>I have a table of Categories. The table can be sorted ascending/descending by clicking the "Name" AJAX anchor on top. The problem is that I can't transfer the current asc/desc state from the view to the controller. The idea is that whenever the user clicks that anchor, the sort type changes. Here is my code:</p>
  
  <p>The <strong>controller</strong>:</p>
  
  <pre><code>public ActionResult Show(string sortBy)
  {
      CategoryDao categoryDao = new CategoryDao();
      var categories = categoryDao.Read().Cast&lt;Category&gt;().ToList();
  
      if (!Request.IsAjaxRequest())
      {
          Session["SortType"] = "nameAsc";
  
          return this.View(categories);
      }
      else
      {
          switch (sortBy)
          {
              case "nameAsc":
                  categories = categories.OrderBy(c =&gt; c.Name).ToList();
                  Session["SortType"] = "nameDesc";
                  break;
              case "nameDesc":
                  categories = categories.OrderByDescending(c =&gt; c.Name).ToList();
                  Session["SortType"] = "nameAsc";
                  break;
              case "productCountAsc":
                  // TODO
                  break;
              case "productCountDesc":
                  // TODO
                  break;
          }
  
          return PartialView("_TableBody", categories);
      }
  }
  </code></pre>
  
  <p>And the <strong>view</strong>(part of it):</p>
  
  <pre><code>@Ajax.ActionLink("Name", "Show", "Category", new { sortBy = Session["SortType"].ToString() },
           new AjaxOptions
           {
               HttpMethod = "GET",
               UpdateTargetId = "table-body",
               InsertionMode = InsertionMode.Replace
           })
  </code></pre>
  
  <p>I tried debugging it and the Session variable keeps its "nameAsc" value on click.. The logic is fine as far as I am concerned. So if you can point out the wrong things in the code and proper fix I would be grateful!</p>