public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { StringBuilder result = new StringBuilder(); if (pagingInfo.CurrentPage - 1 > 0) { TagBuilder prevPage = new TagBuilder("a"); prevPage.MergeAttribute("href", pageUrl(pagingInfo.CurrentPage - 1)); prevPage.InnerHtml = "<"; result.Append(prevPage.ToString()); } for (int i = 1; i <= pagingInfo.TotalPages; i++) { TagBuilder tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) tag.AddCssClass("selected"); result.Append(tag.ToString()); } if (pagingInfo.CurrentPage + 1 <= pagingInfo.TotalPages) { TagBuilder nextPage = new TagBuilder("a"); nextPage.MergeAttribute("href", pageUrl(pagingInfo.CurrentPage + 1)); nextPage.InnerHtml = ">"; result.Append(nextPage.ToString()); } return MvcHtmlString.Create(result.ToString()); }
public ActionResult List(int page = 1) { var snippets = this.snippetRepo.Snippets .IncludeMultiple(x => x.User) .Where(x => x.UserId == webSecurity.CurrentUserId) .OrderByDescending(x => x.DatePublished) .Skip((page - 1) * PageSize) .Take(PageSize) .Select(x => new SnippetVM { Id = x.Id, Title = x.Title, DatePublished = x.DatePublished, }).ToList(); PagingInfo info = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = snippetRepo.Snippets .IncludeMultiple(x => x.User) .Where(x => x.UserId == webSecurity.CurrentUserId) .Count() }; var viewModel = new SnippetListVM { Snippets = snippets, PagingInfo = info }; return View(viewModel); }