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.

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.
In Controllers\StudentController.cs, replace the Index method with the following code:
This code receives a
sortOrderparameter 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 theswitchstatement. When the user clicks a column heading hyperlink, the appropriatesortOrdervalue is provided in the query string.The two
ViewBagvariables are used so that the view can configure the column heading hyperlinks with the appropriate query string values:These are ternary statements. The first one specifies that if the
sortOrderparameter is null or empty,ViewBag.NameSortParmshould 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 descending. If the current order is Date descending, the links must indicate Last Name ascending and Date ascending.The method uses LINQ to Entities to specify the column to sort by. The code creates an
Adding Column Heading Hyperlinks to the Student Index ViewIQueryablevariable before theswitchstatement, modifies it in theswitchstatement, and calls theToListmethod after theswitchstatement. When you create and modifyIQueryablevariables, no query is sent to the database. The query is not executed until you convert theIQueryableobject into a collection by calling a method such asToList. Therefore, this code results in a single query that is not executed until thereturn Viewstatement.In Views\Student\Index.cshtml, replace the
<tr>and<th>elements for the heading row with the following code:Adding a Search Box to the Students Index PageTo add filtering to the Student Index page, you'll add a text box and a submit button to the view and make corresponding changes in the
Adding Filtering Functionality to the Index MethodIndexmethod. The text box will let you enter a string to search for in the first name and last name fields.In Controllers\StudentController.cs, replace the
Indexmethod with the following code:You've added a
searchStringparameter to theIndexmethod. You've also added awhereclause to the LINQ statement that selects only students whose first name or last name contains the search string. The search string value is received from a text box that you'll add later to the Index view. The statement that adds thewhereclause is executed only if there's a value to search for:Run the page, enter a search string, and click Search to verify that filtering is working.
Adding Paging to the Students Index Page
To add paging to the Student Index page, you'll start by installing the PagedList NuGet package. Then you'll make additional changes in the
Indexmethod and add paging links to theIndexview. The following illustration shows the paging links.Installing the PagedList NuGet Package
The NuGet PagedList package installs a
PagedListcollection type. When you put query results in aPagedListcollection, several properties and methods are provided that facilitate paging.In Visual Studio, make sure the project (not the solution) is selected. From the Tools menu, select Library Package Manager and then Add Library Package Reference.
In the Add Library Package Reference dialog box, click the Online tab on the left and then enter "pagedlist" in the search box. When you see the PagedList package, click Install.
Adding Paging Functionality to the Index Method
In Controllers\StudentController.cs, add a
usingstatement for thePagedListnamespace:This code adds a
pageparameter, a current sort order parameter, and a current filter parameter to the method signature, as shown here:The
@modelstatement at the top of the page specifies that the view now gets aPagedListobject instead of aListobject.The text box is initialized with the current search string so that the user can page through filter results without the search string disappearing:
Model.PageNumber is 1, andModel.PageCountis 0.)Run the page.
Click the paging links in different sort orders to make sure paging works. Then enter a search string and try paging again to verify that paging also works correctly with sorting and filtering.
Creating an About Page That Shows Student StatisticsFor the Contoso University website's About page, you'll display how many students have enrolled for each enrollment date. This requires grouping and simple calculations on the groups. To accomplish this, you'll do the following:
Create a view model class for the data that you need to pass to the view. Modify theAboutmethod in theHomecontroller. Modify theAboutview. Creating the View ModelCreate a ViewModels folder. In that folder, create EnrollmentDateGroup.cs and replace the existing code with the following code:
You've now seen how to create a data model and implement basic CRUD, sorting, filtering, paging, and grouping functionality. In the next tutorial you'll begin looking at more advanced topics by expanding the data model.
Links to other Entity Framework resources can be found at the end of the last tutorial in this series.



