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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using mvcpaging.Models;
- using System.Data;
- using PagedList;
- namespace mvcpaging.Controllers
- {
- public class StudentInfoController : Controller
- {
- [HttpGet]
- public ActionResult Index(string sortOrder, string CurrentSort, int? page)
- {
- int pageSize = 5;
- int pageIndex = 1;
- pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;
- ViewBag.CurrentSort = sortOrder;
- sortOrder = String.IsNullOrEmpty(sortOrder) ? "Id" : sortOrder;
- IPagedList<student> stu = null;
- BL buisnessLogic = new BL();
- DataTable dt = new DataTable();
- student objStudent = new student();
- List<student> objStudentList = new List<student>();
- objStudentList = buisnessLogic.studentListBind();
- objStudent.std = objStudentList;
- switch (sortOrder)
- {
- case "Id":
- if (sortOrder.Equals(CurrentSort))
- stu = objStudentList.OrderByDescending
- (m => m.Id).ToPagedList(pageIndex, pageSize);
- else
- stu = objStudentList.OrderBy
- (m => m.Id).ToPagedList(pageIndex, pageSize);
- break;
- case "Name":
- if (sortOrder.Equals(CurrentSort))
- stu = objStudentList.OrderByDescending
- (m => m.Name).ToPagedList(pageIndex, pageSize);
- else
- stu = objStudentList.OrderBy
- (m => m.Name).ToPagedList(pageIndex, pageSize);
- break;
- case "Mother Name":
- if (sortOrder.Equals(CurrentSort))
- stu = objStudentList.OrderByDescending
- (m => m.Mothername).ToPagedList(pageIndex, pageSize);
- else
- stu = objStudentList.OrderBy
- (m => m.Mothername).ToPagedList(pageIndex, pageSize);
- break;
- case "Father Name":
- if (sortOrder.Equals(CurrentSort))
- stu = objStudentList.OrderByDescending
- (m => m.Fathername).ToPagedList(pageIndex, pageSize);
- else
- stu = objStudentList.OrderBy
- (m => m.Fathername).ToPagedList(pageIndex, pageSize);
- break;
- case "Age":
- if (sortOrder.Equals(CurrentSort))
- stu = objStudentList.OrderByDescending
- (m => m.Age).ToPagedList(pageIndex, pageSize);
- else
- stu = objStudentList.OrderBy
- (m => m.Age).ToPagedList(pageIndex, pageSize);
- break;
- case "Country":
- if (sortOrder.Equals(CurrentSort))
- stu = objStudentList.OrderByDescending
- (m => m.Country).ToPagedList(pageIndex, pageSize);
- else
- stu = objStudentList.OrderBy
- (m => m.Country).ToPagedList(pageIndex, pageSize);
- break;
- case "State":
- if (sortOrder.Equals(CurrentSort))
- stu = objStudentList.OrderByDescending
- (m => m.State).ToPagedList(pageIndex, pageSize);
- else
- stu = objStudentList.OrderBy
- (m => m.State).ToPagedList(pageIndex, pageSize);
- break;
- case "Nationality":
- if (sortOrder.Equals(CurrentSort))
- stu = objStudentList.OrderByDescending
- (m => m.Nationality).ToPagedList(pageIndex, pageSize);
- else
- stu = objStudentList.OrderBy
- (m => m.Nationality).ToPagedList(pageIndex, pageSize);
- break;
- }
- return View(stu);
- }
- }
- }
Business logic class(BL.cs)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Data;
- using System.Data.SqlClient;
- namespace mvcpaging.Models
- {
- public class BL
- {
- SqlHelper sql1 = new SqlHelper();
- public List<student> studentListBind() {
- DataTable dt = new DataTable();
- SqlParameter[] GetBalance_Parm = new SqlParameter[]
- {
- };
- DataSet _DS = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "[GetStdInfo]", GetBalance_Parm);
- List<student> lst = new List<student>();
- if (_DS.Tables[0].Rows.Count > 0)
- {
- foreach (DataRow item in _DS.Tables[0].Rows)
- {
- lst.Add(new student
- {
- Id=Convert.ToInt32(item["id"]),
- Name =Convert.ToString(item["name"]),
- Fathername = Convert.ToString(item["fathername"]),
- Mothername = Convert.ToString(item["mothername"]),
- Age = Convert.ToInt32(item["age"]),
- Country = Convert.ToString(item["country"]),
- State = Convert.ToString(item["state"]),
- Nationality = Convert.ToString(item["nationality"])
- });
- }
- }
- return lst;
- }
- }
- }
CSS for paging
- <style>
- .ul.pagination
- {
- display: inline-block;
- padding: 0;
- margin: 0;
- }
- ul.pagination li
- {
- display: inline;
- }
- ul.pagination li a
- {
- color: black;
- float: left;
- padding: 8px 16px;
- text-decoration: none;
- transition: background-color .3s;
- }
- ul.pagination li a.active
- {
- background-color: #4CAF50;
- color: white;
- }
- ul.pagination li a:hover:not(.active)
- {
- background-color: #ddd;
- }
- </style>









Amit KumarPosted Feb 3, 2017, 12:29 AM
Yes, you are correct ,it is possible easily you can check the condition and return list according to your requirement.
Shams RehmanPosted Feb 1, 2017, 2:23 AM
Thanks for useful article but the problem is you add sorting and paging in razor view i need in empty view in which we have own custom table and patch data through j query. So i need pagination in custom table.Thanks once again for this useful article.