Online Training for MVC Web Designing Development, MS SQL, Digital Marketing

Paging in MVC example

Paging is commonly used functionality in any web application, So let’s implement Paging in MVC
Right click on References => Manage NuGet Packages
Search for "PagedList"
Install PagedList.MVC as shown below

Paging in MVC

Required namespace
@using PagedList
@using PagedList.Mvc;

Implement Paging in MVC

Step 1: In Controller Action fetch your data from database, and assign to your IPagedList property in Model
int MaxResultPerPage=50;

public ActionResult Index(int page = 1)
BusinessModel model = new BusinessModel();
string parameter1 = Request.QueryString["parameter1"];
IPagedList<customObject> requirements = null;
using (ServiceDTO rdto = new ServiceDTO())
requirements = rdto.SearchResults(skill, location,true)
.Where(r => r.Location.Contains(parameter1))
.ToPagedList<customObject>(page, MaxResultPerPage);
model.ActiveJobs = requirements;
return View(model);

Step 2: In razor file add the reference of “bootstrappagging.css”, and take the IPagedList property in a variable
<link href="~/css/bootstrappagging.css" rel="stylesheet" />
IPagedList<customObject> objList = Model.ActiveJobs;

Step 3: loop through the collection object and display as per your need
@foreach (customObject j in objList)

// display record as per need


Step 4: Now let’s implement the pagination part
<div style="padding: 4px;">
@Html.PagedListPager(jobs, page => Url.Action("index", new { page }), PagedListRenderOptions.ClassicPlusFirstAndLast)
Enjoy Paging in MVC
Pagination in MVC

Group Training