title: Sorting, Filtering and Paging MVC
body: <p>So essentially I've done all my sorting, filtering and paging with the help of this tutorial, which has been very, very handy because I'm very new to this material. Anyways, I'm having issues now trying to sort and filter a few of my tables which have more than one primary key.</p>
  
  <p><a href="http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application" rel="nofollow">http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application</a></p>
  
  <p>I created the var applications and var databases but I think if I had a way to combine them I wouldn't have issues with my paging. Because my return view would be more concise. </p>
  
  <pre><code>public ActionResult Index(string sortOrder, string searchString, int? page)
      {
          ViewBag.CurrentSort = sortOrder;
          ViewBag.IDSortParm = String.IsNullOrEmpty(sortOrder) ? "AppID_desc" : "";
          ViewBag.NameSortParm = sortOrder == "Name" ? "AppName_desc" : "Name";
          ViewBag.ID2SortParm = sortOrder == "ID" ? "DatabaseID_desc" : "ID";
          ViewBag.Name2SortParm = sortOrder == "Name2" ? "DatabaseName_desc" : "Name2";
  
          if (Request.HttpMethod != "GET")
          {
              page = 1;
          }
          ViewBag.CurrentFilter = searchString;
  
  
          var applications = from a in db.Application_
                        select a;
          var databases = from d in db.Database_ //this is what I added
                          select d;
          if (!String.IsNullOrEmpty(searchString))
          {
              applications = applications.Where(s =&gt; s.AppName.ToUpper().Contains(searchString.ToUpper()));
              databases = databases.Where(d =&gt; d.DatabaseName.ToUpper().Contains(searchString.ToUpper())); //also what I added
          }
          switch (sortOrder)
          {
              case "AppID_desc":
                  applications = applications.OrderByDescending(a =&gt; a.AppID);
                  break;
              case "Name":
                  applications = applications.OrderBy(a =&gt; a.AppName);
                  break;
              case "AppName_desc":
                  applications = applications.OrderByDescending(a =&gt; a.AppName);
                  break;
              case "Name2":
                  databases = databases.OrderBy(d=&gt; d.DatabaseName);
                  break;
              case "DatabaseName_desc":
                  databases = databases.OrderByDescending(d =&gt; d.DatabaseName);
                  break;
              default:
                  applications = applications.OrderBy(a =&gt; a.AppID);
                  break;
  
          }
          int pageSize = 10;
          int pageNumber = (page ?? 1);
          return View(applications.ToPagedList(pageNumber, pageSize));
      }
  </code></pre>
  
  <p>I added the var database because I need to search for values in the database_ table along with the application table.</p>
  
  <p>The index:</p>
  
  <pre><code>@using (Html.BeginForm())
  {
  &lt;p&gt;
      Search Name: @Html.TextBox("Search_Data", ViewBag.FilterValue as string)
      &lt;input type="submit" value="Find" /&gt;
  &lt;/p&gt;
  }
  &lt;table class="table"&gt;
  
  &lt;tr&gt;
      &lt;th&gt;
          @Html.ActionLink("AppID", "Index", new { sortOrder = ViewBag.IDSortParm })
      &lt;/th&gt;
      &lt;th&gt;
          @Html.ActionLink("ApplicationName", "Index", new { sortOrder = ViewBag.NameSortParm })
      &lt;/th&gt;
      &lt;th&gt;
          @Html.ActionLink("DatabaseID", "Index", new { sortOrder = ViewBag.ADSortParm })
      &lt;/th&gt;
      &lt;th&gt;
          @Html.ActionLink("DatabaseName", "Index", new { sortOrder = ViewBag.Name2SortParm })
      &lt;/th&gt;
  </code></pre>
  
  <p>I believe I'm having an issue with the index, but obviously I'm pretty clueless in general so whatever assistance you can offer would be greatly appreciated. </p>
  
  <p>Thanks!!</p>
  
  <p>EDIT:
  For more clarity, plus I found a way to explain myself better.</p>