Skip to content

Paging in ASP.NET Core MVC

Problem definition: When we fetch thousands of records from the database, we cannot show them all in one MVC grid, for readability and performance. We show a limited number of records and add paging with page numbers, so users can move through the records easily. Here we implement that paging logic.

Item list grid with a pagination bar

Pagination bar with First, Previous, page numbers, Next and Last buttons

Implementation: To implement paging, follow the steps below.

  • Create a model class for items.
  • Create a controller named Items.
  • Add this Item model class to the DbContext class and then run the migration commands.
  • Create an action method named Index to list the items.
  • Create a view for the Index method.
  • Create a Pager class for the paging logic.
  • Create a _Pager partial view for the pagination bar.

In the implementation phase, we create a model class for items. Below is the code for the Item model class.

public class Item

   {

       [Key]

       public int ItemId { get; set; }

       public string Name { get; set; }

       public string Brand { get; set; }

       public string Description { get; set; }

       public string Path { get; set; }

       [NotMapped]

       [Display(Name ="Choose Image")]

       public IFormFile ImagePath { get; set; }

   }

Below is the code for the Pager class.

public class Pager

   {

       public int TotalItems { get; set; }

       public int PageSize { get; set; }

       public int CurrentPage { get; set; }

       public int TotalPages { get; set; }

       public int StartPage { get; set; }

       public int EndPage { get; set; }

       public Pager()

       {



       }

       public Pager(int totalItems,int page,int pageSize=5)

       {

           int totalPages=(int)Math.Ceiling((decimal)totalItems/(decimal)pageSize);

           int currentPage = page;

           int startPage = currentPage - 5;

           int endPage = currentPage + 4;

           if(startPage<=0)

           {

               endPage=endPage-(startPage-1);

               startPage = 1;

           }

           if(endPage>totalPages)

           {

               endPage = totalPages;

               if(endPage>10)

               {

                   startPage = endPage - 9;

               }

           }

           TotalItems = totalItems;

           CurrentPage = currentPage;

           PageSize = pageSize;

           TotalPages = totalPages;

           StartPage = startPage;

           EndPage = endPage;

       }

   }

Now create a DbContext class named demoDBcontext and add the DbSet below. It generates the table in the database when you run the migration commands.

public DbSet<Item> Items { get; set; }

Now create a controller named Items and write the action methods below in it. Index() fetches the list of records from the database and shows it in the view.

 public class ItemsController : Controller

    {

        private readonly AmazonShoppingDBContext _db;

        private readonly IWebHostEnvironment webHostEnvironment;

        public ItemsController(AmazonShoppingDBContext db,IWebHostEnvironment environment)

        {

            _db = db;

            webHostEnvironment = environment;



        }



        public IActionResult Index(int pg=1)

        {

            var data = _db.Items.ToList();

            const int pageSize = 5;

            if (pg < 1)

                pg = 1;

            int recsCount = data.Count;



           var pager=new Pager(recsCount,pg,pageSize);

            int recSkip = (pg - 1) * pageSize;

            var dataList=data.Skip(recSkip).Take(pager.PageSize).ToList();  

            ViewBag.Pager=pager;

            return View(dataList);

        }

}

List of Items to present into grid: We implemented an action method, Index, that retrieves the item records from the database table into a list and returns them to the view. Below is the code for the controller action method.

        public IActionResult Index(int pg=1)

        {

            var data = _db.Items.ToList();

            const int pageSize = 5;

            if (pg < 1)

                pg = 1;

            int recsCount = data.Count;



           var pager=new Pager(recsCount,pg,pageSize);

            int recSkip = (pg - 1) * pageSize;

            var dataList=data.Skip(recSkip).Take(pager.PageSize).ToList();  

            ViewBag.Pager=pager;

            return View(dataList);

        }

}

Pager Partial View: This view renders the pagination bar with the First, Previous, Next and Last buttons.

Pager partial view code for the pagination bar

Index View: Design the Index view to show the item list in a grid, and use the pager partial view for the pagination bar, as shown in the image below.

Index view code using the pager partial view

We have now implemented the paging logic; you can test it on your local machine.