private static void PageItem(NavHtmlTextWritter textWriter, MvcHtmlString linkBlock, string cssClass = "") { if (cssClass != string.Empty) { textWriter.AddAttribute(HtmlTextWriterAttribute.Class, cssClass); } textWriter.RenderBeginTag(HtmlTextWriterTag.Li); textWriter.Write(linkBlock); textWriter.RenderEndTag(); }
public static MvcHtmlString PageLinks( this HtmlHelper html, object queryObject, Func<object, string> urlActionDelegate, int linksToShow = 0) { var pagingInfoViewModel = queryObject as IPagingParameters; var result = new StringBuilder(); if (pagingInfoViewModel != null) { var currentPage = pagingInfoViewModel.PageNumber + 1; var totalPages = pagingInfoViewModel.TotalPages; linksToShow = (linksToShow == 0) ? totalPages : linksToShow; if (pagingInfoViewModel.TotalPages <= 1) { return MvcHtmlString.Create(string.Empty); } var stringWriter = new StringWriter(result); string linkBlock = string.Empty; using (var textWriter = new NavHtmlTextWritter(stringWriter)) { { textWriter.AddAttribute(HtmlTextWriterAttribute.Class, Configurations.WebConfigurations.PagingConfigurations.PaginationCssClass); textWriter.RenderBeginTag(HtmlTextWriterTag.Ul); { if (currentPage > 1) { pagingInfoViewModel.PageNumber = 1; PageItem(textWriter, BasePagingExtensions.CreatePageLink(urlActionDelegate, queryObject, Configurations.WebConfigurations.PagingConfigurations.FirstPageText)); pagingInfoViewModel.PageNumber = currentPage - 1; PageItem(textWriter, BasePagingExtensions.CreatePageLink(urlActionDelegate, queryObject, Configurations.WebConfigurations.PagingConfigurations.PreviousPageText)); } // create page links int start = 1; int end = totalPages; start = PagerPounds(linksToShow, totalPages, currentPage, start, ref end); for (int i = start; i <= end; i++) { pagingInfoViewModel.PageNumber = i; if (i == currentPage) { PageItem(textWriter, BasePagingExtensions.CreatePageLink(urlActionDelegate, queryObject, i.ToString()), Configurations.WebConfigurations.PagingConfigurations.ActivePageClass); } else { PageItem(textWriter, BasePagingExtensions.CreatePageLink(urlActionDelegate, queryObject, i.ToString())); } } // create 'Next >>' link if (currentPage < totalPages) { pagingInfoViewModel.PageNumber = currentPage + 1; PageItem(textWriter, BasePagingExtensions.CreatePageLink(urlActionDelegate, queryObject, Configurations.WebConfigurations.PagingConfigurations.NextPageText)); pagingInfoViewModel.PageNumber = totalPages; PageItem(textWriter, BasePagingExtensions.CreatePageLink(urlActionDelegate, queryObject, Configurations.WebConfigurations.PagingConfigurations.LastPageText)); } textWriter.RenderEndTag(); } } } } return MvcHtmlString.Create(result.ToString()); }