public void Can_generate_links_to_other_pages() { HtmlHelper html = null; var paging = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10, }; Func<int, string> url = i => "Page" + i; MvcHtmlString result = html.PageLinks(paging, url); Assert.AreEqual(result.ToString(), @"<ul> <li> <a href=""Page1"">1</a> </li> <li class=""selected""> <a href=""Page2"">2</a> </li> <li> <a href=""Page3"">3</a> </li> </ul>"); }
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl, object attributes = null) { var xml = new XElement("ul", // Add any root attributes from kvp in new RouteValueDictionary(attributes) select new XAttribute(kvp.Key, kvp.Value), // Add an li for each page from i in Enumerable.Range(1, pagingInfo.TotalPages) select new XElement("li", i == pagingInfo.CurrentPage? new XAttribute("class", "selected"):null, // set the class attribute to "selected" for the current page new XElement("a", new XAttribute("href", pageUrl(i)), i) ) ); return MvcHtmlString.Create(xml.ToString()); }
public void PagingInfo_calculates_total_pages_correctly() { var pagingInfo = new PagingInfo { TotalItems = 27, ItemsPerPage = 10 }; Assert.AreEqual(pagingInfo.TotalPages, 3); }