public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= pagingInfo.TotalPages; i++) { TagBuilder tag = new TagBuilder("a"); // construct an <a> tag tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) tag.AddCssClass("selected"); result.AppendLine(tag.ToString()); } return MvcHtmlString.Create(result.ToString()); }
public void Can_Generate_Links_To_Other_Pages() { // ARRANGE: html helper class can be null HtmlHelper html = null; // ARRANGE: the helper will take a PagingInfo instance and a lambda to specify the URL PagingInfo pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; Func<int, string> pageUrl = i => "Page" + i; // ACT: switch the page?? MvcHtmlString result = html.PageLinks(pagingInfo, pageUrl); // ASSERT: verify format of the page/url result.ToString().ShouldEqual(@"<a href=""Page1"">1</a> <a class=""selected"" href=""Page2"">2</a> <a href=""Page3"">3</a> "); }