日期:2014-05-17  浏览次数:20831 次

Entity Framework in ASP.NET MVC Application (三)

In the previous tutorial you implemented a set of web pages for basic CRUD operations for Student entities. In this tutorial you'll add sorting, filtering, and paging functionality to the Students Index page. You'll also create a page that does simple grouping.

The following illustration shows what the page will look like when you're done. The column headings are links that the user can click to sort by that column. Clicking a column heading repeatedly toggles between ascending and descending sort order.

Students_Index_page_with_paging

Adding Column Sort Links to the Students Index Page

To add sorting to the Student Index page, you'll change the Index method of the Student controller and add code to the Student Index view.

Adding Sorting Functionality to the Index Method

In Controllers\StudentController.cs, replace the Index method with the following code:

public ViewResult Index(string sortOrder)

{

    ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";

    ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";

    var students = from s in db.Students

                   select s;

    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(students.ToList());

}

This code receives a sortOrder parameter from the query string in the URL, which is provided by ASP.NET MVC as a parameter to the action method. The parameter will be a string that's either "Name" or "Date", optionally followed by a space and the string "desc" to specify descending order.

The first time the Index page is requested, there's no query string. The students are displayed in ascending order by LastName, which is the default as established by the fall-through case in the switch statement. When the user clicks a column heading hyperlink, the appropriate sortOrder value is provided in the query string.

The two ViewBag variables are used so that the view can configure the column heading hyperlinks with the appropriate query string values:

ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";

ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";

These are ternary statements. The first one specifies that if the sortOrder parameter is null or empty, ViewBag.NameSortParm should be set to "Name desc"; otherwise, it should be set to an empty string.

There are four possibilities, depending on how the data is currently sorted:

  • If the current order is Last Name ascending, the Last Name link must specify Last Name descending, and the Enrollment Date link must specify Date ascending.
  • If the current order is Last Name descending, the links must indicate Last Name ascending (that is, empty string) and Date ascending.
  • If the current order is Date ascending, the links must indicate Last Name ascending and Date