locked
sort filtered objects RRS feed

  • Question

  • User1237607600 posted

    Hey,

    I just finished the tutorial 

    https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-2.2#add-filtering-functionality-to-the-index-method

    I'm sure I did everything the explained way, but when I search for the students with an 'A' and than want to sort the results, all students are shown again.

    Does anybody know, what I'm doing wrong?

    Controller:

    public async Task<IActionResult> Index(string sortOrder, string searchString)
    {
    ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
    ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";
    ViewData["CurrentFilter"] = searchString;

    var students = from s in _context.Students
    select s;
    if (!String.IsNullOrEmpty(searchString))
    {
    students = students.Where(s => s.LastName.Contains(searchString)
    || s.FirstMidName.Contains(searchString));
    }
    switch (sortOrder)
    {
    case "name_desc":
    students = students.OrderByDescending(s => s.LastName);
    break;
    case "Date":
    students = students.OrderBy(s => s.EnrollmentDate);
    break;
    case "date_desc":
    students = students.OrderByDescending(s => s.EnrollmentDate);
    break;
    default:
    students = students.OrderBy(s => s.LastName);
    break;
    }
    return View(await students.AsNoTracking().ToListAsync());
    }

    View

    <form asp-action="Index" method="get">
    <div class="form-actions no-color">
    <p>
    Find by name: <input type="text" name="SearchString" value="@ViewData["currentFilter"]" />
    <input type="submit" value="Search" class="btn btn-default" /> |
    <a asp-action="Index">Back to Full List</a>
    </p>
    </div>
    </form>


    <table class="table">
    <thead>
    <tr>
    <th>
    <a asp-action="Index" asp-route-sortOrder="@ViewData["NameSortParm"]">@Html.DisplayNameFor(model => model.LastName)</a>
    </th>
    <th>
    @Html.DisplayNameFor(model => model.FirstMidName)
    </th>
    <th>
    <a asp-action="Index" asp-route-sortOrder="@ViewData["DateSortParm"]">@Html.DisplayNameFor(model => model.EnrollmentDate)</a>
    </th>
    <th></th>
    </tr>
    </thead>

    Thursday, August 8, 2019 9:25 AM

Answers

  • User753101303 posted

    Hi,

    Seems you are missing asp-route-currentFilter="@ViewData["CurrentFilter"]" on  the sorting links.

    I often suggest to always take a couple of minutes to see what happens possibly with the debugger (ie the filter is missing, likely in the browser address bar as well and then you look at how the url is constructed) rather than reading code and wondering why it fails which is often longer.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 8, 2019 11:01 AM

All replies

  • User753101303 posted

    Hi,

    Seems you are missing asp-route-currentFilter="@ViewData["CurrentFilter"]" on  the sorting links.

    I often suggest to always take a couple of minutes to see what happens possibly with the debugger (ie the filter is missing, likely in the browser address bar as well and then you look at how the url is constructed) rather than reading code and wondering why it fails which is often longer.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 8, 2019 11:01 AM
  • User1237607600 posted

    Thanks, that helps a lot! I recognized that the address bar was wrong, I just hadn't have an idea how to change it.

    Finally, it was asp-route-searchString that worked.

    Thursday, August 8, 2019 11:22 AM