title: How do I add a sorting feature to my MVC asp.net Core app?
body: <p>I have an MVC app where users can log their create entries and see them in a table on their screen. I would like to add a feature to where the users can click on on of the <strong>Name</strong> header and the table sort by the name column. I am using <a href="https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-2.0" rel="nofollow noreferrer">this docs.Microsoft tutorial</a> to help me but I don't seem to be getting anywhere. Here's what I have so far:</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Index(string sortOrder)
      {
          ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
          ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";
          var logs = from l in _context.Log
                          select l;
          switch (sortOrder)
          {
              case "name_desc":
                  logs = logs.OrderByDescending(l =&gt; l.Name);
                  break;
              case "Date":
                  logs = logs.OrderBy(l =&gt; l.Date);
                  break;
              case "date_desc":
                  logs = logs.OrderByDescending(l =&gt; l.Date);
                  break;
              case "relation":
                  logs = logs.OrderByDescending(l =&gt; l.Relation);
                  break;
              case "type":
                  logs = logs.OrderByDescending(l =&gt; l.Type);
                  break;
              default:
                  logs = logs.OrderBy(l =&gt; l.Date);
                  break;
          }
          return View(await logs.AsNoTracking().ToListAsync());
      }
  
      public async Task OnGetAsync(string parentComment, string searchString)
      {
          // Use LINQ to get list of dates.
          IQueryable&lt;DateTime&gt; dateQuery = from m in _context.Log
                                          // orderby m.Date
                                          select m.Date;
  
          var logs = from l in _context.Log
                       select l;
  
          if (!String.IsNullOrEmpty(searchString))
          {
              logs = logs.Where(s =&gt; s.Name.Contains(searchString));
          }
  
          if (!String.IsNullOrEmpty(parentComment))
          {
              logs = logs.Where(x =&gt; x.Type == parentComment);
          }
          Dates = new SelectList(await dateQuery.Distinct().ToListAsync());
          Log = await logs.ToListAsync();
      }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;table class="table"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.Log[0].Date)
              &lt;/th&gt;
              &lt;th&gt;
              &lt;a asp-action="Index" asp-route-sortOrder='@ViewData["NameSortParm"]'&gt;
                  @Html.DisplayNameFor(model =&gt; model.Log[0].Name)
              &lt;/a&gt;
          &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.Log[0].Relation)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.Log[0].Type)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.Log[0].Comment)
              &lt;/th&gt;
              &lt;th&gt;&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          @foreach (var item in Model.Log)
          {
              &lt;tr&gt;
                  &lt;th&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Date)
                  &lt;/th&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Name)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Relation)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Type)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Comment)
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;a asp-page="./Edit" asp-route-id="@item.ID"&gt;Edit&lt;/a&gt; |
                      &lt;a asp-page="./Details" asp-route-id="@item.ID"&gt;Details&lt;/a&gt; |
                      &lt;a asp-page="./Delete" asp-route-id="@item.ID"&gt;Delete&lt;/a&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          }
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>As you can see, I have it set up for the <strong>Name</strong> column in the view. When I click on it, the URL changes to let me know that the <code>Index()</code> method fired (<a href="https://localhost:5001/HSLogs?action=Index" rel="nofollow noreferrer">https://localhost:5001/HSLogs?action=Index</a>) and the page reloads. No sorting happens. Hardly anything happens. Thoughts?</p>