How To Use Paging And Sorting In ASP.NET MVC 4

In this blog, I will explain the process of paging and sorting in ASP.NET MVC 4 step by step. The database is uploaded in this article.
 
Open a new project


 
Click OK button. 



Select empty template and for paging, install PagedList.mvc in your project but in my project, it is already installed.

If PagedList.mvc is not installed in your project, install it,  as it is needed to see previous articles.

 
Hence, there is no need to install it again.

Its reference is added in controller.



Add student class(student.cs).



Add a controller StudentInfo code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using mvcpaging.Models;  
  7. using System.Data;  
  8. using PagedList;  
  9. namespace mvcpaging.Controllers  
  10. {  
  11.     public class StudentInfoController : Controller  
  12.     {  
  13.        [HttpGet]  
  14.         public ActionResult Index(string sortOrder, string CurrentSort, int? page)  
  15.         {  
  16.             int pageSize = 5;  
  17.             int pageIndex = 1;  
  18.             pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;  
  19.   
  20.             ViewBag.CurrentSort = sortOrder;  
  21.   
  22.             sortOrder = String.IsNullOrEmpty(sortOrder) ? "Id" : sortOrder;  
  23.   
  24.             IPagedList<student> stu = null;  
  25.             BL buisnessLogic = new BL();  
  26.   
  27.   
  28.             DataTable dt = new DataTable();  
  29.   
  30.             student objStudent = new student();  
  31.             List<student> objStudentList = new List<student>();  
  32.             objStudentList = buisnessLogic.studentListBind();  
  33.               
  34.             objStudent.std = objStudentList;  
  35.             switch (sortOrder)  
  36.             {  
  37.                 case "Id":  
  38.                     if (sortOrder.Equals(CurrentSort))  
  39.                         stu = objStudentList.OrderByDescending  
  40.                                 (m => m.Id).ToPagedList(pageIndex, pageSize);  
  41.                     else  
  42.                         stu = objStudentList.OrderBy  
  43.                                 (m => m.Id).ToPagedList(pageIndex, pageSize);  
  44.                     break;  
  45.   
  46.                 case "Name":  
  47.                     if (sortOrder.Equals(CurrentSort))  
  48.                         stu = objStudentList.OrderByDescending  
  49.                                 (m => m.Name).ToPagedList(pageIndex, pageSize);  
  50.                     else  
  51.                         stu = objStudentList.OrderBy  
  52.                                 (m => m.Name).ToPagedList(pageIndex, pageSize);  
  53.                     break;  
  54.   
  55.                 case "Mother Name":  
  56.                     if (sortOrder.Equals(CurrentSort))  
  57.                         stu = objStudentList.OrderByDescending  
  58.                                 (m => m.Mothername).ToPagedList(pageIndex, pageSize);  
  59.                     else  
  60.                         stu = objStudentList.OrderBy  
  61.                                 (m => m.Mothername).ToPagedList(pageIndex, pageSize);  
  62.                     break;  
  63.   
  64.                 case "Father Name":  
  65.                     if (sortOrder.Equals(CurrentSort))  
  66.                         stu = objStudentList.OrderByDescending  
  67.                                 (m => m.Fathername).ToPagedList(pageIndex, pageSize);  
  68.                     else  
  69.                         stu = objStudentList.OrderBy  
  70.                                 (m => m.Fathername).ToPagedList(pageIndex, pageSize);  
  71.                     break;  
  72.   
  73.                 case "Age":  
  74.                     if (sortOrder.Equals(CurrentSort))  
  75.                         stu = objStudentList.OrderByDescending  
  76.                                 (m => m.Age).ToPagedList(pageIndex, pageSize);  
  77.                     else  
  78.                         stu = objStudentList.OrderBy  
  79.                                 (m => m.Age).ToPagedList(pageIndex, pageSize);  
  80.                     break;  
  81.   
  82.                 case "Country":  
  83.                     if (sortOrder.Equals(CurrentSort))  
  84.                         stu = objStudentList.OrderByDescending  
  85.                                 (m => m.Country).ToPagedList(pageIndex, pageSize);  
  86.                     else  
  87.                         stu = objStudentList.OrderBy  
  88.                                 (m => m.Country).ToPagedList(pageIndex, pageSize);  
  89.                     break;  
  90.                 case "State":  
  91.                     if (sortOrder.Equals(CurrentSort))  
  92.                         stu = objStudentList.OrderByDescending  
  93.                                 (m => m.State).ToPagedList(pageIndex, pageSize);  
  94.                     else  
  95.                         stu = objStudentList.OrderBy  
  96.                                 (m => m.State).ToPagedList(pageIndex, pageSize);  
  97.                     break;  
  98.                 case "Nationality":  
  99.                     if (sortOrder.Equals(CurrentSort))  
  100.                         stu = objStudentList.OrderByDescending  
  101.                                 (m => m.Nationality).ToPagedList(pageIndex, pageSize);  
  102.                     else  
  103.                         stu = objStudentList.OrderBy  
  104.                                 (m => m.Nationality).ToPagedList(pageIndex, pageSize);  
  105.                     break;  
  106.             }  
  107.             return View(stu);                    
  108.         }  
  109.     }  
  110. }  
Business logic class(BL.cs) 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data;  
  6. using System.Data.SqlClient;  
  7.   
  8. namespace mvcpaging.Models  
  9. {  
  10.     public class BL  
  11.     {  
  12.         SqlHelper sql1 = new SqlHelper();  
  13.   
  14.         public List<student> studentListBind() {  
  15.             DataTable dt = new DataTable();  
  16.             SqlParameter[] GetBalance_Parm = new SqlParameter[]               
  17.             {   
  18.                
  19.             };  
  20.             DataSet _DS = SqlHelper.GetDataSet(SqlHelper.mainConnectionString,                                CommandType.StoredProcedure, "[GetStdInfo]", GetBalance_Parm);  
  21.             List<student> lst = new List<student>();  
  22.             if (_DS.Tables[0].Rows.Count > 0)  
  23.             {  
  24.                 foreach (DataRow  item in _DS.Tables[0].Rows)  
  25.                 {  
  26.                     lst.Add(new student  
  27.                     {  
  28.                         Id=Convert.ToInt32(item["id"]),  
  29.                         Name =Convert.ToString(item["name"]),  
  30.                         Fathername = Convert.ToString(item["fathername"]),  
  31.                         Mothername = Convert.ToString(item["mothername"]),  
  32.                         Age = Convert.ToInt32(item["age"]),  
  33.                         Country = Convert.ToString(item["country"]),  
  34.                         State = Convert.ToString(item["state"]),  
  35.                         Nationality = Convert.ToString(item["nationality"])  
  36.   
  37.                     });  
  38.                 }  
  39.                 
  40.             }  
  41.             return lst;  
  42.         }  
  43.           
  44.     }  
  45. }  

CSS for paging 

  1. <style>  
  2.     .ul.pagination  
  3.     {  
  4.         display: inline-block;  
  5.         padding0;  
  6.         margin0;  
  7.     }  
  8.   
  9.     ul.pagination li  
  10.     {  
  11.         displayinline;  
  12.     }  
  13.   
  14.         ul.pagination li a  
  15.         {  
  16.             colorblack;  
  17.             floatleft;  
  18.             padding8px 16px;  
  19.             text-decorationnone;  
  20.             transition: background-color .3s;  
  21.         }  
  22.   
  23.             ul.pagination li a.active  
  24.             {  
  25.                 background-color#4CAF50;  
  26.                 colorwhite;  
  27.             }  
  28.   
  29.             ul.pagination li a:hover:not(.active)  
  30.             {  
  31.                 background-color#ddd;  
  32.             }  
  33. </style>   
View
  1. @using PagedList.Mvc  
  2. @model  PagedList.IPagedList<mvcpaging.Models.student>  
  3.   
  4. @{  
  5.     ViewBag.Title = "Student Details";  
  6. }  
  7.   
  8. <style>  
  9.     .ul.pagination  
  10.     {  
  11.         display: inline-block;  
  12.         padding: 0;  
  13.         margin: 0;  
  14.     }  
  15.   
  16.     ul.pagination li  
  17.     {  
  18.         display: inline;  
  19.     }  
  20.   
  21.         ul.pagination li a  
  22.         {  
  23.             color: black;  
  24.             float: left;  
  25.             padding: 8px 16px;  
  26.             text-decoration: none;  
  27.             transition: background-color .3s;  
  28.         }  
  29.   
  30.             ul.pagination li a.active  
  31.             {  
  32.                 background-color: #4CAF50;  
  33.                 color: white;  
  34.             }  
  35.   
  36.             ul.pagination li a:hover:not(.active)  
  37.             {  
  38.                 background-color: #ddd;  
  39.             }  
  40. </style>  
  41. @using (Html.BeginForm())  
  42. {  
  43.     <center>  
  44.          <h2>Student Details</h2>  
  45.     <div  style="border:1px solid red; margin-left:25%; margin-right:20%">  
  46.     <table style="width:100%" border="1">  
  47.         <tr>  
  48.   
  49.             <th style="/*/*/*width:15%; text-align:left*/*/*/"> @Html.ActionLink("Student ID", "Index",  
  50.             new { sortOrder = "Id"CurrentSort = ViewBag.CurrentSort })</th>  
  51.             <th style="width:10%; text-align:left">@Html.ActionLink("Name", "Index",  
  52.             new { sortOrder = "Name"CurrentSort = ViewBag.CurrentSort })</th>  
  53.             <th style="width:15%; text-align:left">@Html.ActionLink("Father Name", "Index",  
  54.             new { sortOrder = "Father Name"CurrentSort = ViewBag.CurrentSort })</th>  
  55.             <th style="width:15%; text-align:left">@Html.ActionLink("Mother Name", "Index",  
  56.             new { sortOrder = "Mother Name"CurrentSort = ViewBag.CurrentSort })</th>  
  57.             <th style="width:5%; text-align:left">@Html.ActionLink("Age", "Index",  
  58.             new { sortOrder = "Age"CurrentSort = ViewBag.CurrentSort })</th>  
  59.             <th style="width:15%; text-align:left">@Html.ActionLink("Country", "Index",  
  60.             new { sortOrder = "Country"CurrentSort = ViewBag.CurrentSort })</th>  
  61.             <th style="width:15%; text-align:left">@Html.ActionLink("State", "Index",  
  62.             new { sortOrder = "State"CurrentSort = ViewBag.CurrentSort })</th>  
  63.             <th style="width:15%; text-align:left">@Html.ActionLink("Nationality", "Index",  
  64.             new { sortOrder = "Nationality"CurrentSort = ViewBag.CurrentSort })</th>  
  65.   
  66.                         
  67.         </tr>  
  68.   
  69.    @for (int i = 0; i < Model.Count; i++)  
  70.    {  
  71.        <tr style="border:1px solid red">  
  72.            <td>@Model[i].Id</td>  
  73.            <td>@Model[i].Name</td>  
  74.            <td>@Model[i].Fathername</td>  
  75.            <td>@Model[i].Mothername</td>  
  76.            <td>@Model[i].Age</td>  
  77.            <td>@Model[i].Country</td>  
  78.            <td>@Model[i].State</td>  
  79.            <td>@Model[i].Nationality</td>  
  80.        </tr>  
  81.          
  82.    }  
  83.  </table>  
  84.        
  85.      
  86.     </div>  
  87.           </center>  
  88.     <div id="container" style="margin-left: 20px">  
  89.         <p></p>  
  90.         <p></p>  
  91.         <div class="pagination" style="margin-left: 400px">  
  92.             Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)  
  93.             of @Model.PageCount   @Html.PagedListPager(Model, page => Url.Action("Index", new { page }))  
  94.         </div>  
  95.     </div>  
  96. }  
After executing it, first we are talking about paging, followed by sorting. The default first page opens, which is given below and can be viewed via Student Id. 


Now, after clicking on the second page, its data is showing here. 


Now, click on 4th page and it's data is shown here. 


Now, you have seen that paging is working fine.
 
Now, we will see sorting here. When the project is executed, then you can see student ID 


After clicking on Student ID header, it is sorted in descending order, which you can  see.


For Name sorting, you can see the name. 


After clicking on Name header, it's sorted, which you can see.

Here, sorting for Age is given below. 


After clicking on Age header, it's sorted, which you can see below. 


Here, as you can see, every thing is working fine. In this blog, we have learned how to use paging and sorting in ASP.NET MVC 4. 
Ebook Download
View all
Learn
View all