The Fool
The Fool

Reputation: 93

Table sorting is not working

I'm following a tutorial on www.asp.net and have got to a point where I am trying to sort a table of results. However it doesn't sort once I click on the links to order them to sort. Can anyone help with this?

Controller:

 public ActionResult Index(string sortOrder)
        {
            ViewBag.FirstNameSortParm = String.IsNullOrEmpty(sortOrder) ? "firstName_desc" : "";
            ViewBag.MiddleNameSortParm = String.IsNullOrEmpty(sortOrder) ? "middleName_desc" : "";
            ViewBag.LastNameSortParm = String.IsNullOrEmpty(sortOrder) ? "lastName_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

            var students = from s in db.Students
                           select s;

            switch (sortOrder)
            {
                 case "firstName_desc":
                    students = students.OrderByDescending(s => s.FirstName);
                    break; 
                case "middleName_desc":
                    students = students.OrderByDescending(s => s.MiddleName);
                    break;
                case "lastName_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(db.Students.ToList());
        }

View:

<table class="table">
    <tr>
        <th>
           @Html.ActionLink("First Name", "Index", new { sortOrder = ViewBag.FirstNameSortParm })
        </th>
        <th>
            @Html.ActionLink("Middle Name", "Index", new { sortOrder = ViewBag.MiddleNameSortParm })
        </th>
        <th>
            @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.LastNameSortParm })
        </th>
        <th>
            @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MiddleName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EnrollmentDate)
        </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>

Upvotes: 0

Views: 73

Answers (1)

The Fool
The Fool

Reputation: 93

found the problem myself. The problem was with this line of code:

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

The answer should be:

return View(students.ToList());

This happened because I was retrieving my results from the database, telling the program to sort the name and then I was returning the names from the database compared to returning the object students which contained the sort instructions.

Upvotes: 0

Related Questions