This article includes the following sections:
- Sort (sorting)
- Search (searching)
- Paging (Paging)
Sort (sorting)
This section demonstrates sorting with Student entities as an example.
Add sort function to Controller
Step 1: Open the StudentController.cs so that the Index () method is as follows:
Public ActionResult Index (string sorting_order) { viewbag.sortingname = String.IsNullOrEmpty (sorting_order)? "Name_description": ""; Viewbag.sortingdate = Sorting_order = = "Date_enroll"? "Date_description": "Date"; var students = from Stu in db. Students Select Stu;switch (sorting_order) {case "name_description": Students = Students. OrderByDescending (stu=> Stu. FirstName); Break;case "Date_enroll": students = students. (Stu = stu. enrollmentdate); Break;case "Date_description": students = students. OrderByDescending (stu = stu. enrollmentdate); Break;default: students = students. (Stu = stu. FirstName); break; } Return View (students. ToList ());}
The parameter sorting_order in the code is dynamically fetched in the URL. The string type, which is ascending by default.
Add a sort hyperlink to a table header in a view
Step 2: open views\student\index.cshtml , modify the code as follows:
<p> @Html. ActionLink ("Create New", "create") </p><table class= "table" ><tr><th> @ Html.ActionLink ("First Name", "Index", new {Sorting_order = Viewbag.sortingname}) </th><th> last name& Lt;/th><th> @Html. ActionLink ("Enrollment Date", "Index", new {Sorting_order = viewbag.sortingdate}) < /th><th></th></tr> @foreach (var item in Model) {
Step 3: run the viewing effect.
Step 4: Click the first Name column in the header to see the sorting function.
Search (searching)
Adding the query function to the controller
Step 1: open StudentController.cs , modify the Index () method as follows:
Public ActionResult Index (string sorting_order, String search_data) {viewbag.sortingname = String.IsNullOrEmpty (Sortin G_order)? "Name_description": ""; Viewbag.sortingdate = Sorting_order = = "Date_enroll"? "Date_description": "Date"; var students = from Stu in db. Students Select Stu; {students = students. Where (stu = stu. Firstname.toupper (). Contains (Search_data.toupper ()) | | Stu. Lastname.toupper (). Contains (Search_data.toupper ())); }switch (sorting_order) {case "name_description": Students = students. OrderByDescending (stu=> Stu. FirstName); Break;case "Date_enroll": Students = students. (Stu = stu. enrollmentdate); Break;case "date_description": Students = students. OrderByDescending (stu = stu. enrollmentdate); break;default:students = students. (Stu = stu. FirstName); break; }return View (students. ToList ());}
In the code we have added
Search_dataparameters and use LINQ expressions.
Add a Query button to a view
Step 2: open views\student\index.cshtml , modify the code as follows:
<p> @Html. ActionLink ("Create New", "create") </p> @using (Html.BeginForm ()) {<p> Search Name: @ Html.textbox ("Search_data", Viewbag.filtervalue As String) <input type= "Submit" value= "Find"/></p>}< Table class= "Table" >
Step 3: Run the program and enter the search criteria.
View search Results
Paging (Paging)
To demonstrate the paging feature, we'll use the PAGEDLIST.MVC control, which you can get installed through NuGet.
Installing the PAGEDLIST.MVC NuGet package
Step 1: Open the package management platform, "Tools", "Library Package Manager".
Step 2: Enter the following command:
Install-package PAGEDLIST.MVC
Add paging feature
Step 3: open StudentController.cs and add the following namespaces:
Using Pagedlist;
Step 4: Modify the Index () method as follows:
Public ActionResult Index (string sorting_order, String search_data, string filter_value, int?) PAGE_NO) {viewbag.currentsortorder = Sorting_order; Viewbag.sortingname = String.IsNullOrEmpty (sorting_order)? "Name_description": ""; Viewbag.sortingdate = Sorting_order = = "Date_enroll"? "Date_description": "Date"; if (search_data! = null) {page_no = 1; }else {search_data = Filter_value; } viewbag.filtervalue = Search_data;var students = from Stu in db. Students Select Stu;if (! String.IsNullOrEmpty (Search_data)) {students = students. Where (stu = stu. Firstname.toupper (). Contains (Search_data.toupper ()) | | Stu. Lastname.toupper (). Contains (Search_data.toupper ())); }switch (sorting_order) {case "name_description": Students = students. OrderByDescending (stu=> Stu. FirstName); Break;case "Date_enroll": Students = students. (Stu = stu. enrollmentdate); Break;case "date_description": Students = StudenTs. OrderByDescending (stu = stu. enrollmentdate); break;default:students = students. (Stu = stu. FirstName); break; }int Size_of_page = 4;int No_of_page = (page_no?? 1); return View (students. Topagedlist (No_of_page, Size_of_page));}
Add a paging control to a view
Step 5: open views\student\index.cshtml , modify the code as follows:
@model pagedlist.ipagedlist<vag_infotech.models.student> @using pagedlist.mvc;<link href= "~/Content/ Pagedlist.css "rel=" stylesheet "/>@{viewbag.title =" Students ";} Attention@modelType byThe list type object becomes pagedlistThe type object.Step 6: run the program to see the paging effect.
Step 7: Try the search function again.
Summarize
This article demonstrates the ability to sort, query, and page out in ASP.
Implementation of ASP. MVC4 Sorting Retrieval page