Skip to content

Instantly share code, notes, and snippets.

@aisyshk
Last active April 25, 2023 12:21
Show Gist options
  • Save aisyshk/6333c10581019d212e971b37d5c77406 to your computer and use it in GitHub Desktop.
Save aisyshk/6333c10581019d212e971b37d5c77406 to your computer and use it in GitHub Desktop.
ASP.NET Core Pagination (w/ X.PagedList)
@model IEnumerable<MyProject.Models.MyObject>
@{
ViewData["Title"] = "MyObjects";
}
<div class="text-center">
<div class="text-center">
<table class="table table-striped">
<tr>
<th scope="col">MyProperty</th>
<th scope="col">MyProperty2</th>
</tr>
@foreach (var obje in ViewBag.PageWan)
{
<tr>
<td scope="row" style="text-align: left;">@obje.MyProperty</td>
<td>@obje.MyProperty2</td>
</tr>
}
</table>
<div class="text-center">
<p>
Pages:
<br />
@for (int i = 1; i < (ViewBag.AllPages + 1); i++)
{
// Style this to your liking
<a style="text-decoration: none;" asp-controller="PaginatedContentController" asp-action="Index" asp-route-page="@i">
<span class="btn-secondary" style="border-radius: 0px; padding: 1px 3px 1px 3px; color: white;">@i</span>
</a>
}
</p>
</div>
</div>
</div>
namespace MyProject.Models
{
public class MyObject
{
public Type MyProperty { get; set; }
public Type MyProperty2 { get; set; }
}
}
using MyProject.Models
using Microsoft.AspNetCore.Mvc;
using System.Globalization;
using X.PagedList;
// ... Your database dependencies
namespace MyProject.Controllers
{
public class PaginatedContentController : Controller
{
// Data from a database or a .csv/.tsv file
IEnumerable<MyObject> MyObjects;
// The page that will be displayed
IEnumerable<MyObject> PageOne;
// All other pages
List<IEnumerable<MyObject>> TotalPages;
// Max data per page
public const int MaxPerPage = 35;
// Amount of available pages. Ex: 30
public int AvailablePages;
// Index of previous page, in short CurrentPageIndex - 1
public int PreviousPageIndex;
// Current page number as an index
public int CurrentPageIndex;
// Index of next page, in short CurrentPageIndex + 1
public int NextPageIndex;
public void GetAllMyObjects()
{
// ...
// Load your data here and sort it to MyObjects IEnumerable.
// MyObjects = [...].ToList();
}
public async Task<IActionResult> Index(int? page)
{
// Calling to allocate data
GetAllMyObjects();
// This section gets all the indexes for pagination.
#region Pagination Indexing
CurrentPageIndex = page ?? 1;
if (CurrentPageIndex < 1 || page < 1) { CurrentPageIndex = (int)page; }
var internalPageIndex = CurrentPageIndex - 1;
if (internalPageIndex <= 0) { internalPageIndex = 1; }
if (PreviousPageIndex - 1 == 0) { PreviousPageIndex = PreviousPageIndex + 1; }
else { PreviousPageIndex = CurrentPageIndex - 1; }
if (!(NextPageIndex + 1 <= AvailablePages)) { NextPageIndex = CurrentPageIndex + 1; }
else if (NextPageIndex + 1 > AvailablePages) { NextPageIndex = CurrentPageIndex; }
#endregion
// Calculate how many pages there should be
var totalSplit = Math.Round(Math.Ceiling((decimal)MyObjects.Count() / (decimal)MaxPerPage));
// Splits MyObjects to different pages
var splitPage = MyObjects.Split((int)totalSplit).ToList();
// Displays first page with MyObjects
PageOne = splitPage[internalPageIndex].ToList();
// Count how many total pages there are
for (int i = 0; i < splitPage.Count(); i++)
{
AvailablePages++;
}
// Pass data to view
ViewBag.AllPages = AvailablePages;
ViewBag.PreviousPage = PreviousPageIndex;
ViewBag.CurrentPage = CurrentPageIndex;
ViewBag.NextPage = NextPageIndex;
ViewBag.PageWan = PageOne;
return View();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment