Created
November 29, 2017 11:10
-
-
Save shturm/a13231a1b62997fc822b9300420d5998 to your computer and use it in GitHub Desktop.
AspNetCore pagination
This file contains 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
@* include Url.Current() extension method *@ | |
@using MyProject.Web.Misc | |
@model MyProject.Web.Models.Pager | |
<!-- pager --> | |
@if (Model.EndPage > 1) | |
{ | |
<ul class="pagination"> | |
@if (Model.CurrentPage > 1) | |
{ | |
<li> | |
<a href="@Url.Current(new { pageNumber = 1 })">First</a> | |
</li> | |
<li> | |
<a href="@Url.Current(new { pageNumber = Model.CurrentPage - 1 })">Previous</a> | |
</li> | |
} | |
@for (int page = Model.StartPage; page <= Model.EndPage; page++) | |
{ | |
<li class="@(page == Model.CurrentPage ? "active" : "")"> | |
<a href="@Url.Current(new { pageNumber = page })">@(page)</a> | |
</li> | |
} | |
@if (Model.CurrentPage < Model.TotalPages) | |
{ | |
<li> | |
<a href="@Url.Current(new { pageNumber = Model.CurrentPage + 1 })">Next</a> | |
</li> | |
<li> | |
<a href="@Url.Current(new { pageNumber = Model.TotalPages })">Last</a> | |
</li> | |
} | |
</ul> | |
} | |
<!-- /pager --> |
This file contains 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 class Pager | |
{ | |
public Pager(int totalItems, int? page, int pageSize = 10) | |
{ | |
// calculate total, start and end pages | |
var totalPages = (int)Math.Ceiling((decimal)totalItems / (decimal)pageSize); | |
var currentPage = page != null ? (int)page : 1; | |
var startPage = currentPage - 5; | |
var endPage = currentPage + 4; | |
if (startPage <= 0) | |
{ | |
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; | |
} | |
public int TotalItems { get; private set; } | |
public int CurrentPage { get; private set; } | |
public int PageSize { get; private set; } | |
public int TotalPages { get; private set; } | |
public int StartPage { get; private set; } | |
public int EndPage { get; private set; } | |
} |
This file contains 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 static class UrlHelperExtensions | |
{ | |
/// <summary> | |
/// Get current URL with some substituted values. Preserves query string. | |
/// </summary> | |
/// <param name="helper"></param> | |
/// <param name="substitutes"></param> | |
/// <returns></returns> | |
public static string Current(this IUrlHelper helper, object substitutes) | |
{ | |
RouteValueDictionary routeData = new RouteValueDictionary(helper.ActionContext.RouteData.Values); | |
IQueryCollection queryString = helper.ActionContext.HttpContext.Request.Query; | |
//add query string parameters to the route data | |
foreach (var param in queryString) | |
{ | |
if (!string.IsNullOrEmpty(queryString[param.Key])) | |
{ | |
//rd[param.Key] = qs[param.Value]; // does not assign the values! | |
routeData.Add(param.Key, param.Value); | |
} | |
} | |
// override parameters we're changing in the route data | |
//The unmatched parameters will be added as query string. | |
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(substitutes.GetType())) | |
{ | |
var value = property.GetValue(substitutes); | |
if (string.IsNullOrEmpty(value.ToString())) | |
{ | |
routeData.Remove(property.Name); | |
} | |
else | |
{ | |
routeData[property.Name] = value; | |
} | |
} | |
string url = helper.RouteUrl(routeData); | |
return url; | |
} | |
} |
This file contains 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
@model SomeModelWithPagerProperty | |
@* current page items *@ | |
@{ Html.RenderPartial("_PagerPartial", Model.Pager); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment