Example #1
0
        public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl)
        {
            var result = new StringBuilder();
            for (int i = 1; i <= pagingInfo.TotalPages; i++)
            {
                var 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.Append(tag.ToString());
            }
            return MvcHtmlString.Create(result.ToString());
        }
Example #2
0
 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
     var 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(result.ToString(), @"<a href=""Page1"">1</a>"
                                        + @"<a class=""selected"" href=""Page2"">2</a>"
                                        + @"<a href=""Page3"">3</a>");
 }