Created
January 12, 2024 16:58
-
-
Save thepuskar/cd3fa10e84b311ffc95c0c8e6d49f9d4 to your computer and use it in GitHub Desktop.
Pagination in C# Asp.net Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public IActionResult Index(int pageNumber = 1, int pageSize = 10) | |
{ | |
IEnumerable<Category> categoryList = _db.Categories.ToList(); | |
if (pageNumber < 1) pageNumber = 1; | |
int totalItems = categoryList.Count(); | |
var pager = new Pager(totalItems, pageNumber, pageSize); | |
int recSkip = (pageNumber - 1) * pageSize; | |
var data = categoryList.Skip(recSkip).Take(pager.PageSize).ToList(); | |
this.ViewBag.Pager = pager; | |
return View(data); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int pageNumber = 0; | |
if (ViewBag.Pager != null) | |
{ | |
pager = ViewBag.Pager; | |
pageNumber = pager.CurrentPage; | |
} | |
<--HTML--> | |
@if (pager.TotalPages > 0) | |
{ | |
<ul class="pagination pagination-sm mb-0"> | |
<li class="page-item @(pager.CurrentPage > 1 ? "":"disabled")"> | |
<a class="page-link" asp-controller="Category" asp-action="Index" , asp-route-pageNumber="@(pager.CurrentPage -1 )">«</a> | |
</li> | |
@for (var pge = pager.StartPage; pge <= pager.EndPage; pge++) | |
{ | |
<li class="page-item @(pge == pager.CurrentPage ? "active":"")"> | |
<a class="page-link" asp-controller="Category" asp-action="Index" asp-route-pageNumber="@pge">@pge</a> | |
</li> | |
} | |
<li class="page-item @(pager.CurrentPage < pager.TotalPages ?"":"disabled")"> | |
<a class="page-link" asp-controller="Category" asp-action="Index" asp-route-pageNumber="@(pager.CurrentPage+1)">»</a> | |
</li> | |
</ul> | |
} | |
<--HTML--> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace bulkyBook.Models | |
{ | |
public class Pager | |
{ | |
public int TotalItems { get; set; } | |
public int CurrentPage { get; set; } | |
public int PageSize { 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) | |
{ | |
int totalPages = (int)Math.Ceiling((decimal)totalItems / (decimal)pageSize); | |
int currentPage = page; | |
int startPage = currentPage - 5; | |
int endPage = currentPage + 5; | |
if (startPage <= 0) | |
{ | |
endPage = endPage - (startPage - 1); | |
startPage = 1; | |
} | |
if (endPage > totalPages) | |
{ | |
endPage = totalPages; | |
if (endPage > 10) | |
{ | |
startPage = endPage - 10; | |
} | |
} | |
TotalItems = totalItems; | |
CurrentPage = currentPage; | |
PageSize = pageSize; | |
TotalPages = totalPages; | |
StartPage = startPage; | |
EndPage = endPage; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment