Created
January 21, 2020 15:17
-
-
Save garpunkal/a5c4ee52b0bfaaff4bfea27b76597017 to your computer and use it in GitHub Desktop.
DisplayTemplate for MvcPaging
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 PaginationModel | |
@if (Model == null || Model.PageCount <= 1) { return; } | |
<ul class="pagination"> | |
@foreach (var link in Model.PaginationLinks) | |
{ | |
@BuildLink(link) | |
} | |
</ul> | |
@helper BuildLink(PaginationLink link) | |
{ | |
var liBuilder = new TagBuilder("li"); | |
liBuilder.AddCssClass("pagination__item"); | |
if (link.IsCurrent) | |
{ | |
liBuilder.AddCssClass("is-active"); | |
} | |
if (!link.Active) | |
{ | |
liBuilder.AddCssClass("is-disabled"); | |
} | |
TagBuilder aBuilder = new TagBuilder(link.Active ? "a" : "span"); | |
aBuilder.AddCssClass("pagination__link"); | |
if (link.Active) | |
{ | |
if (link.IsCurrent) | |
{ | |
aBuilder.Attributes["href"] = "#"; | |
} | |
else | |
{ | |
aBuilder.Attributes["href"] = $"?p={link.PageIndex}"; | |
} | |
} | |
aBuilder.SetInnerText(link.DisplayText); | |
liBuilder.InnerHtml = aBuilder.ToString(); | |
@Html.Raw(liBuilder.ToString()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment