MarkJClark
MarkJClark

Reputation: 75

MVC5 sorting and filtering

I have been trying to implement a sort on a project and am running into difficulties. I have looked at various references at how to accomplish this but it still won't work in my project. I have looked at

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

and

http://www.c-sharpcorner.com/UploadFile/4b0136/perform-paging-searching-sorting-in-Asp-Net-mvc-5/

What I have is my controller

  public ActionResult Index(string sortBy, string searchString)
    {
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortBy) ? "Surname desc" : "";
        ViewBag.DateSort = sortBy == "StartDate" ? "date desc" : "StartDate";
        var students = from s in db.Students
                       select s;
        if (!String.IsNullOrEmpty(searchString))
        {
            students = students.Where(s => s.Surname.Contains(searchString));
        }

        switch (sortBy)
        {
            case "name_desc":
                students = students.OrderByDescending(s => s.Surname);
                break;
            case "Date":
                students = students.OrderBy(s => s.StartDate);
                break;
            case "date_desc":
                students = students.OrderByDescending(s => s.StartDate);
                break;
            default:
                students = students.OrderBy(s => s.Surname);
                break;
        }

        return View(db.Students.ToList());
    }

and my view

    @using (Html.BeginForm())
{
    <p>
        Find by name: @Html.TextBox("SearchString")
        <input type="submit" value="Search" />
    </p>
}
<table class="table">
    <tr>

        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.ActionLink("Surname", "Index", new { sortBy = ViewBag.NameSortParm})
        </th>
        <th>
            @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSort})
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Surname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.StartDate)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

 </table>

This gives me the action links in the view and what seems to be the right once clicked on localhost:5841/students?sortBy=Surname%20desc and localhost:5841/students?sortOrder=StartDate. My issue is that they don't change and sort as they should. Am I missing something?

Thanks

Upvotes: 1

Views: 1819

Answers (1)

user3559349
user3559349

Reputation:

Your doing the sorting in the controller method, but then you finally just return an unsorted collection using

return View(db.Students.ToList());

which calls the database again. Instead, change the return statement to

return View(students); // or students.ToList() depending on the model in the view

to return the sorted collection.

Upvotes: 1

Related Questions