Skip to content

Instantly share code, notes, and snippets.

@garpunkal
Created January 21, 2020 15:17
Show Gist options
  • Save garpunkal/a5c4ee52b0bfaaff4bfea27b76597017 to your computer and use it in GitHub Desktop.
Save garpunkal/a5c4ee52b0bfaaff4bfea27b76597017 to your computer and use it in GitHub Desktop.
DisplayTemplate for MvcPaging
@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