Paging And Sorting In ASP.NET MVC

Introduction

In this article, we will learn how we can implement paging and sorting in MVC. In this article for paging we will use PageList.MVC package which we will download from Nuget Package Manager. I will create one table using code first approach of Entity Framework.
 
The following step will explain to you how can we perform paging and sorting in MVC.
 
Firstly, open Visual Studio and create an MVC project by clicking File, New, Project or press CTRL + SHIFT + N Key together. 
 
 
 
After clicking on New Project you will get one dialog box. From that dialog box go to installed template and Visual C# and then web and choose ASP.NET Web Application and give the name to your project. Press OK, also you can follow the below figure.
 
 
After clicking on OK button you will get one more dialog box where you have to select your project template so select project template as MVC with No Authentication.

 
 
After completing the above steps your project will ready. Now By default your project will contain Home Controller. So, delete HomeController and Inside View folder you will get Home Folder so also Delete Home Folder from the project.
 
After deleting right click on the solution and download Entity Framework from the Nuget Package Manager. Here are the steps for getting Entity Framework package in your solution. 
  1. Right click on project and click on Manage NuGet Packages.



  2. After that you will get one more dialog box. On that dialog box click on online and search for Entity Framework.



After installing entity framework in my project I am creating a new folder with name "Entities" and also adding one class inside that folder with the name "EmployeeMaster".

Because I am creating this project with code first approach I am creating the table with the name EmployeeMaster so I have added this class. I am writing the following code inside that class.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6.   
  7. namespace PagingAndSorting.Entities  
  8. {  
  9.     public class EmployeeMaster  
  10.     {  
  11.         [Key]  
  12.         public string ID { getset; }  
  13.         [Required(ErrorMessage="Please Enter Employee Name")]  
  14.         public string Name { getset; }  
  15.         [Required(ErrorMessage="Please Enter Phone Number")]  
  16.         public string PhoneNumber { getset; }  
  17.         [Required(ErrorMessage="Please Enter Email")]  
  18.         public string Email { getset; }  
  19.         [Required(ErrorMessage="Please Enter Salary")]  
  20.         public decimal Salary { getset; }  
  21.     }  
  22. }  
After writing the above code in your project build your project once and go to the Modal folder and add a class with name ApplicationDbContext.
 
Write the following code inside ApplicationDbContext class.
  1. using PagingAndSorting.Entities;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Data.Entity;  
  5. using System.Linq;  
  6. using System.Web;  
  7.   
  8.   
  9. namespace PagingAndSorting.Models  
  10. {  
  11.     public class ApplicationDbContext:DbContext  
  12.     {  
  13.         public ApplicationDbContext()  
  14.             : base("DefaultConnection")  
  15.         {  
  16.         }  
  17.         public DbSet<EmployeeMaster> Employees { getset; }  
  18.     }  
  19. }  
After adding that code in ApplicationDbContext class add connection string in web.config. I am adding the following connection string in my code. 
  1. <connectionStrings>  
  2.    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=EmployeeDb;Integrated Security=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />  
  3.  </connectionStrings>  
Now to generate database and table open package manager console. You can open package manager console by performing the following steps.
  1. Click on Tools strip on Menu Bar.
  2. Select Library Package Manager.
  3. Then Select Package Manager Console.
After opening package manager console. Type the following command,
  1. PM> Enable-Migrations  
After completing this process one folder will be generated in your solution that one's name will be Migrations. There will be one file Configurations.cs.  Open that file and inside Configuration class constructor set AutomaticMigrationsEnabled to true.
 
Configurations.cs
  1. namespace PagingAndSorting.Migrations  
  2. {  
  3.     using System;  
  4.     using System.Data.Entity;  
  5.     using System.Data.Entity.Migrations;  
  6.     using System.Linq;  
  7.   
  8.     internal sealed class Configuration : DbMigrationsConfiguration<PagingAndSorting.Models.ApplicationDbContext>  
  9.     {  
  10.         public Configuration()  
  11.         {  
  12.             AutomaticMigrationsEnabled = true;  
  13.         }  
  14.   
  15.         protected override void Seed(PagingAndSorting.Models.ApplicationDbContext context)  
  16.         {  
  17.             //  This method will be called after migrating to the latest version.  
  18.   
  19.             //  You can use the DbSet<T>.AddOrUpdate() helper extension method   
  20.             //  to avoid creating duplicate seed data. E.g.  
  21.             //  
  22.             //    context.People.AddOrUpdate(  
  23.             //      p => p.FullName,  
  24.             //      new Person { FullName = "Andrew Peters" },  
  25.             //      new Person { FullName = "Brice Lambson" },  
  26.             //      new Person { FullName = "Rowan Miller" }  
  27.             //    );  
  28.             //  
  29.         }  
  30.     }  
  31. }  
Now open package manager console and firethe following command which will generate database and tables.
  1. PM> update-database  
After generating database for performing paging you have to download PagedList.MVC from NuGet Package manager, so go to the NuGet Package Manager and then install PagedList.MVC.

 
 
Now add a controller with name EmployeeController and write the following code. 
  1. using PagingAndSorting.Entities;  
  2. using PagingAndSorting.Models;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Mvc;  
  8. using PagedList;  
  9. namespace PagingAndSorting.Controllers  
  10. {  
  11.     public class EmployeeController : Controller  
  12.     {  
  13.         //  
  14.         // GET: /Employee/  
  15.         public ActionResult Index(string sortOrder, string CurrentSort, int? page)  
  16.         {  
  17.             ApplicationDbContext db = new ApplicationDbContext();  
  18.             int pageSize = 10;  
  19.             int pageIndex = 1;  
  20.             pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;  
  21.             ViewBag.CurrentSort = sortOrder;  
  22.             sortOrder = String.IsNullOrEmpty(sortOrder) ? "Name" : sortOrder;  
  23.             IPagedList<EmployeeMaster> employees = null;  
  24.             switch (sortOrder)  
  25.             {  
  26.                 case "Name":  
  27.                     if (sortOrder.Equals(CurrentSort))  
  28.                         employees = db.Employees.OrderByDescending  
  29.                                 (m => m.Name).ToPagedList(pageIndex, pageSize);  
  30.                     else  
  31.                         employees = db.Employees.OrderBy  
  32.                                 (m => m.Name).ToPagedList(pageIndex, pageSize);  
  33.                     break;  
  34.                 case "Email":  
  35.                     if (sortOrder.Equals(CurrentSort))  
  36.                         employees = db.Employees.OrderByDescending  
  37.                                 (m => m.Email).ToPagedList(pageIndex, pageSize);  
  38.                     else  
  39.                         employees = db.Employees.OrderBy  
  40.                                 (m => m.Email).ToPagedList(pageIndex, pageSize);  
  41.                     break;  
  42.                 case "Phone":  
  43.                     if (sortOrder.Equals(CurrentSort))  
  44.                         employees = db.Employees.OrderByDescending  
  45.                                 (m => m.PhoneNumber).ToPagedList(pageIndex, pageSize);  
  46.                     else  
  47.                         employees = db.Employees.OrderBy  
  48.                                 (m => m.PhoneNumber).ToPagedList(pageIndex, pageSize);  
  49.                     break;  
  50.                 case "Salary":  
  51.                     if (sortOrder.Equals(CurrentSort))  
  52.                         employees = db.Employees.OrderByDescending  
  53.                                 (m => m.Salary).ToPagedList(pageIndex, pageSize);  
  54.                     else  
  55.                         employees = db.Employees.OrderBy  
  56.                                 (m => m.Salary).ToPagedList(pageIndex, pageSize);  
  57.                     break;  
  58.                 case "Default":  
  59.                     employees = db.Employees.OrderBy  
  60.                         (m => m.Name).ToPagedList(pageIndex, pageSize);  
  61.                     break;  
  62.             }  
  63.             return View(employees);  
  64.         }  
  65.   
  66.   
  67.         public ActionResult Add()  
  68.         {  
  69.             return View();  
  70.         }  
  71.   
  72.         [HttpPost]  
  73.         [ValidateAntiForgeryToken]  
  74.         public ActionResult Add(EmployeeMaster emp)  
  75.         {  
  76.             emp.ID = Guid.NewGuid().ToString();  
  77.             ApplicationDbContext db = new ApplicationDbContext();  
  78.             db.Employees.Add(emp);  
  79.             db.SaveChanges();  
  80.             return RedirectToAction("Index");  
  81.         }  
  82.     }  
  83.  
Now add two views, One for adding some Employee and second for Views Employee and Paging and Sorting. I am adding a View for Adding Employee with name Add.  And write the following code in Add View. 
  1. @model PagingAndSorting.Entities.EmployeeMaster  
  2.   
  3. @{  
  4.     ViewBag.Title = "Add Employee";  
  5. }  
  6.   
  7. <h2>Add</h2>  
  8.   
  9.   
  10. @using (Html.BeginForm())   
  11. {  
  12.     @Html.AntiForgeryToken()  
  13.       
  14.     <div class="form-horizontal">  
  15.         <h4>EmployeeMaster</h4>  
  16.         <hr />  
  17.         @Html.ValidationSummary(true)  
  18.         <div class="form-group">  
  19.             @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })  
  20.             <div class="col-md-10">  
  21.                 @Html.EditorFor(model => model.Name)  
  22.                 @Html.ValidationMessageFor(model => model.Name)  
  23.             </div>  
  24.         </div>  
  25.   
  26.         <div class="form-group">  
  27.             @Html.LabelFor(model => model.PhoneNumber, new { @class = "control-label col-md-2" })  
  28.             <div class="col-md-10">  
  29.                 @Html.EditorFor(model => model.PhoneNumber)  
  30.                 @Html.ValidationMessageFor(model => model.PhoneNumber)  
  31.             </div>  
  32.         </div>  
  33.   
  34.         <div class="form-group">  
  35.             @Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" })  
  36.             <div class="col-md-10">  
  37.                 @Html.EditorFor(model => model.Email)  
  38.                 @Html.ValidationMessageFor(model => model.Email)  
  39.             </div>  
  40.         </div>  
  41.   
  42.         <div class="form-group">  
  43.             @Html.LabelFor(model => model.Salary, new { @class = "control-label col-md-2" })  
  44.             <div class="col-md-10">  
  45.                 @Html.EditorFor(model => model.Salary)  
  46.                 @Html.ValidationMessageFor(model => model.Salary)  
  47.             </div>  
  48.         </div>  
  49.   
  50.         <div class="form-group">  
  51.             <div class="col-md-offset-2 col-md-10">  
  52.                 <input type="submit" value="Create" class="btn btn-default" />  
  53.             </div>  
  54.         </div>  
  55.     </div>  
  56. }  
  57.   
  58. <div>  
  59.     @Html.ActionLink("Back to List""Index")  
  60. </div>  
  61.   
  62. @section Scripts {  
  63.     @Scripts.Render("~/bundles/jqueryval")  
  64. }  
And for Viewing list of Employee we have Index Controller so I am adding view with name Index. Write the following code for Index View. 
  1. @model PagedList.IPagedList<PagingAndSorting.Entities.EmployeeMaster>  
  2. @using PagedList.Mvc;  
  3. @{  
  4.     ViewBag.Title = "Employee List";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7. <style>  
  8.     table {  
  9.         width: 100%;  
  10.     }  
  11.     table tr td{  
  12.         border: 2px solid black;  
  13.         text-align: center;  
  14.         word-wrap: break-word;  
  15.     }  
  16.   
  17.     table tr:hover {  
  18.         background-color:#000;  
  19.         color:#fff;  
  20.     }  
  21.     table tr th {  
  22.         border: 2px solid black;  
  23.         text-align: center;  
  24.         background-color: #fff;  
  25.         color: #000;  
  26.     }  
  27. </style>  
  28. <h2>Employee List</h2>  
  29.   
  30. @using (Html.BeginForm())  
  31. {  
  32.     <table>  
  33.         <tr>  
  34.             <th>  
  35.                 @Html.ActionLink("Employee Name""Index",  
  36.                 new { sortOrder = "Name", CurrentSort = ViewBag.CurrentSort })  
  37.             </th>  
  38.             <th>  
  39.                 @Html.ActionLink("Email""Index",  
  40.                 new { sortOrder = "Email", CurrentSort = ViewBag.CurrentSort })  
  41.             </th>  
  42.             <th>  
  43.                 @Html.ActionLink("PhoneNumber""Index",  
  44.                 new { sortOrder = "Phone", CurrentSort = ViewBag.CurrentSort })  
  45.             </th>  
  46.             <th>  
  47.                 @Html.ActionLink("Salary""Index",  
  48.                 new { sortOrder = "Salary", CurrentSort = ViewBag.CurrentSort })  
  49.             </th>  
  50.         </tr>  
  51.         @foreach (var item in Model)  
  52.         {  
  53.             <tr>  
  54.                 <td>  
  55.                     @Html.DisplayFor(modelItem => item.Name)  
  56.                 </td>  
  57.                 <td>  
  58.                     @Html.DisplayFor(modelItem => item.Email)  
  59.                 </td>  
  60.                 <td>  
  61.                     @Html.DisplayFor(modelItem => item.PhoneNumber)  
  62.                 </td>  
  63.                 <td>  
  64.                     @Html.DisplayFor(modelItem => item.Salary)  
  65.                 </td>  
  66.             </tr>  
  67.         }  
  68.     </table>  
  69.     <br />  
  70.     <div id='Paging' style="text-align:center">  
  71.         Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)  
  72.         of @Model.PageCount  
  73.   
  74.         @Html.PagedListPager(Model, page => Url.Action("Index"new { page }))  
  75.     </div>  
  76. }  
Now I am changing Default URL for my Application in Route.Config.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7.   
  8. namespace PagingAndSorting  
  9. {  
  10.     public class RouteConfig  
  11.     {  
  12.         public static void RegisterRoutes(RouteCollection routes)  
  13.         {  
  14.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  15.   
  16.             routes.MapRoute(  
  17.                 name: "Default",  
  18.                 url: "{controller}/{action}/{id}",  
  19.                 defaults: new { controller = "Employee", action = "Index", id = UrlParameter.Optional }  
  20.             );  
  21.         }  
  22.     }  
  23. }  
And I am changing in Menu Bar under _Layout.cshtml
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  6.     <title>@ViewBag.Title - My ASP.NET Application</title>  
  7.     @Styles.Render("~/Content/css")  
  8.     @Scripts.Render("~/bundles/modernizr")  
  9. </head>  
  10. <body>  
  11.     <div class="navbar navbar-inverse navbar-fixed-top">  
  12.         <div class="container">  
  13.             <div class="navbar-header">  
  14.                 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">  
  15.                     <span class="icon-bar"></span>  
  16.                     <span class="icon-bar"></span>  
  17.                     <span class="icon-bar"></span>  
  18.                 </button>  
  19.                 @Html.ActionLink("Employee Master", "Index", "Employee", null, new { @class = "navbar-brand" })  
  20.             </div>  
  21.             <div class="navbar-collapse collapse">  
  22.                 <ul class="nav navbar-nav">  
  23.                     <li>@Html.ActionLink("View Employee", "Index", "Employee")</li>  
  24.                     <li>@Html.ActionLink("Add Employee ", "Add", "Employee")</li>  
  25.                 </ul>  
  26.             </div>  
  27.         </div>  
  28.     </div>  
  29.     <div class="container body-content">  
  30.         @RenderBody()  
  31.         <hr />  
  32.         <footer>  
  33.             <p>© @DateTime.Now.Year - Employee Master</p>  
  34.         </footer>  
  35.     </div>  
  36.   
  37.     @Scripts.Render("~/bundles/jquery")  
  38.     @Scripts.Render("~/bundles/bootstrap")  
  39.     @RenderSection("scripts", required: false)  
  40. </body>  
  41. </html>  
Output:
Add Employee 

 
 
List of Employee With Paging and Sorting

  
Complete Demo 
 
 
Note: You can download this article code from the following link.
 
Read more articles on ASP.NET:


Similar Articles