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 <a> tag tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) tag.AddCssClass("selected"); result.Append(tag.ToString()); } return MvcHtmlString.Create(result.ToString()); }
public void PageLinksTest_CanGeneratePageLinks() { HtmlHelper htmlHelper = null; PagingInfo pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; Func<int, string> pageUrlDelegate = i => "Page" + i; MvcHtmlString expected = MvcHtmlString.Create(@"<a href=""Page1"">1</a><a class=""selected"" href=""Page2"">2</a><a href=""Page3"">3</a>"); MvcHtmlString actual = PagingHelpers.PageLinks(htmlHelper, pagingInfo, pageUrlDelegate); Assert.AreNotEqual(expected, actual);//two different objects Assert.AreEqual(expected.ToString(), actual.ToString()); }
public void PageLinks_WhenGivenAPagingInformation_ShouldReturnASeriesOfHtmlLinksForEveryPage() { // Arrange HtmlHelper helper = null; PagingInfo pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; Func<int, string> pageUrlDelegate = x => "Page" + x; // Act MvcHtmlString links = helper.PageLinks(pagingInfo, pageUrlDelegate); // Assert Assert.AreEqual(expected: links.ToString(), actual: @"<a href=""Page1"">1</a><a class=""selected"" href=""Page2"">2</a><a href=""Page3"">3</a>"); }
public static MvcHtmlString PagingLinks(this HtmlHelper helper, PagingInfo pageInfo, Func<int, string> calculatePageUrl) { StringBuilder pagingString = new StringBuilder(); for (int page = 1; page <= pageInfo.TotalPages; page++) { TagBuilder anchorTag = new TagBuilder("a"); anchorTag.MergeAttribute("href", calculatePageUrl(page)); anchorTag.InnerHtml = page.ToString(); if (page == pageInfo.CurrentPage) { anchorTag.AddCssClass("selected"); } pagingString.AppendLine(anchorTag.ToString()); } return MvcHtmlString.Create(pagingString.ToString()); }
public void Can_Generate_Page_Links() { //potrzebujemy zdefiniowanej metody pomocniczej html HtmlHelper myHelper = null; PagingInfo pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; Func<int, string> pageUrlDelegate = i => "strona" + i; MvcHtmlString result = myHelper.PageLinks(pagingInfo, pageUrlDelegate); Assert.AreEqual(@"<a class=""btn btn-default"" href=""strona1"">1</a>" + @"<a class=""btn btn-default btn-primary selected"" href=""strona2"">2</a>" + @"<a class=""btn btn-default"" href=""strona3"">3</a>", result.ToString()); }
public void Can_Generate_Page_Links() { HtmlHelper myHelper = null; //arrange PagingInfo pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; // setup lambda Func<int, string> pageUrlDelegate = i => "Page" + i; // act MvcHtmlString result = myHelper.PageLinks(pagingInfo, pageUrlDelegate); // assert Assert.AreEqual(@"<a class=""btn btn-default"" href=""Page1"">1</a>" + @"<a class=""btn btn-default btn-primary selected"" href=""Page2"">2</a>" + @"<a class=""btn btn-default"" href=""Page3"">3</a>", result.ToString()); }
public void Can_Generate_Page_Links() { // Arrange - define an HTML helper - we need to do this // in order to apply the extension method HtmlHelper myHelper = null; // Arrange - create PagingInfo data PagingInfo pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; // Arrange - set up the delegate using a lambda expression Func<int, string> pageUrlDelegate = i => "Page" + i; // Act MvcHtmlString result = myHelper.PageLinks(pagingInfo, pageUrlDelegate); // Assert Assert.AreEqual(@"<a class=""btn btn-default"" href=""Page1"">1</a>" + @"<a class=""btn btn-default btn-primary selected"" href=""Page2"">2</a>" + @"<a class=""btn btn-default"" href=""Page3"">3</a>", result.ToString()); }
public void can_generate_links_to_other_pages() { // arrange HtmlHelper helper = null; PagingInfo info = new PagingInfo { CurrentPage = 2, TotalItems = 14, ItemsPerPage = 5 }; Func<int, string> pageUrl = page => "Page" + page; // act MvcHtmlString result = helper.PagingLinks(info, pageUrl); // assert result.ToString().ShouldEqual(@"<a href=""Page1"">1</a>" + Environment.NewLine + @"<a class=""selected"" href=""Page2"">2</a>" + Environment.NewLine + @"<a href=""Page3"">3</a>" + Environment.NewLine + ""); }
public void Can_Send_Paginaton_View_Model() { //arrange Mock<IProductsRepository> mock = new Mock<IProductsRepository>(); mock.Setup(m => m.Products).Returns(new Product[]{ new Product{ProductID = 1, Name="P1"}, new Product{ProductID = 2, Name="P2"}, new Product{ProductID = 3, Name="P3"}, new Product{ProductID = 4, Name="P4"}, new Product{ProductID = 5, Name="P5"} }); ProductController controller = new ProductController(mock.Object); controller.pageSize = 3; //act ProductsListViewModel result = (ProductsListViewModel)controller.List(null, 2).Model; //assert PagingInfo pageInfo = new PagingInfo(); Assert.AreEqual(pageInfo.CurrentPage, 2); Assert.AreEqual(pageInfo.ItemsPerPage, 3); Assert.AreEqual(pageInfo.TotalItems, 5); Assert.AreEqual(pageInfo.TotalItems, 2); }