title: How to sort a Grid using Viewmodel not ViewBag?
body: <p>I have my code working and sorting correctly but I am using ViewBag to do so. I would like to clean up my code and use ViewModel instead to sort my grid but I'm stumped in how to do so. Any help would be greatly appreciated. Thank you.</p>
  
  <p><strong>Model</strong></p>
  
  <pre><code>public partial class Asset
      {
          public int AssetKey { get; set; }
          public int ProductKey { get; set; }
          public string InventoryOwner { get; set; }
          public string SerialNumber { get; set; }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public class InventoryManagementController : Controller
      {
          private Orders db = new Orders();
  
  
          public ActionResult Index(int? AssetNum, string keyword, string sortOrder, string currentFilter, int? page)
          {
  
              ViewBag.OwnerSort = sortOrder == "owner_asce" ? "owner_desc" : "owner_asce";
              ViewBag.AssetSort = sortOrder == "asset_asce" ? "asset_desc" : "asset_asce";
              ViewBag.SerialSort = sortOrder == "serialnum_asce" ? "serialnum_desc" : "serialnum_asce";
              ViewBag.ProductSort = sortOrder == "product_asce" ? "product_desc" : "product_asce";
  
              ViewBag.Keyword = keyword;
              var records = from s in db.Assets select s;
              string AssetNums = AssetNum.ToString();
              if (keyword != null)
              {
                  page = 1;
              }
              else
              {
                  keyword = currentFilter;
              }
              ViewBag.CurrentFilter = keyword;
  
              switch (sortOrder)
              {
  
                  case "asset_asce":
                      records = records.OrderBy(s =&gt; s.AssetKey);
                      break;
  
                  case "asset_desc":
                      records = records.OrderByDescending(s =&gt; s.AssetKey);
                      break;
  
                  case "serialnum_asce":
                      records = records.OrderBy(s =&gt; s.SerialNumber);
                      break;
  
                  case "serialnum_desc":
                      records = records.OrderByDescending(s =&gt; s.SerialNumber);
                      break; 
  
                  case "product_asce":
                      records = records.OrderBy(s =&gt; s.Product.ProductName);
                      break;
  
                  case "product_desc":
                      records = records.OrderByDescending(s =&gt; s.Product.ProductName);
                      break;
  
                  case "owner_asce":
                      records = records.OrderBy(s =&gt; s.InventoryOwner);
                      break;
  
                  case "owner_desc":
                      records = records.OrderByDescending(s =&gt; s.InventoryOwner);
                      break;
  
              int pageSize = 25; //Number of Records
              int pageNumber = (page ?? 1);
  
              return View(records.ToPagedList(pageNumber, pageSize));
              //return View(assets.ToList());
          }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model PagedList.IPagedList&lt;OrderDB.Models.Asset&gt;
  @using PagedList.Mvc;
  &lt;link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" /&gt;
  
  @{
      ViewBag.Title = "Inventory HomePage";
  }
  
  &lt;!DOCTYPE html&gt;
  
      &lt;title&gt;Index&lt;/title&gt;
      &lt;br /&gt;
     &lt;h2&gt;Inventory&lt;/h2&gt;
  
          &lt;table class="table" border="1"&gt;
              &lt;tr&gt;
                  &lt;th&gt;
                      @Html.ActionLink("Asset Tag", "Index", new { sortOrder = ViewBag.AssetSort, currentFilter = ViewBag.CurrentFilter })
                  &lt;/th&gt;
                  &lt;th&gt;
                      @Html.ActionLink("Serial Number", "Index", new { sortOrder = ViewBag.SerialSort, keyword = ViewBag.Keyword, currentFilter = ViewBag.CurrentFilter })
                  &lt;/th&gt;
                  &lt;th&gt;
                      @Html.ActionLink("Product", "Index", new { sortOrder = ViewBag.ProductSort, keyword = ViewBag.Keyword, currentFilter = ViewBag.CurrentFilter })
                  &lt;/th&gt;     
                  &lt;th&gt;
                      @Html.ActionLink("Inventory Owner", "Index", new { sortOrder = ViewBag.OwnerSort, keyword = ViewBag.Keyword, currentFilter = ViewBag.CurrentFilter })
                  &lt;/th&gt;
              &lt;/tr&gt;
  
              @foreach (var item in Model)
              {
                  &lt;tr&gt;
                      &lt;td&gt;
                          @Html.ActionLink(item.AssetKey.ToString(), "Details", new { id = item.AssetKey })
                      &lt;/td&gt;
                      &lt;td&gt;
                          @Html.DisplayFor(modelItem =&gt; item.SerialNumber)
                      &lt;/td&gt;
                      &lt;td&gt;
                          @Html.DisplayFor(modelItem =&gt; item.Product.ProductName)
                      &lt;/td&gt;
                      &lt;td&gt;
                          @Html.DisplayFor(modelItem =&gt; item.InventoryOwner)
                      &lt;/td&gt;
                  &lt;/tr&gt;
              }
          &lt;/table&gt;
          &lt;br /&gt;
          Page @(Model.PageCount &lt; Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
  
          @Html.PagedListPager(Model, page =&gt; Url.Action("Index",
      new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
  </code></pre>