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

CSS for paging

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