public void Can_generate_page_links()
        {
            HtmlHelper htmlHelper = null;

            var pagingInfo = new PagingInfo
            {
                CurrentPage = 2,
                TotalItems = 28,
                ItemsPerPage = 10
            };

            Func<int, string> pageUrlDelegate = i => "page" + i;

            var htmlString = htmlHelper.PageLinks(pagingInfo, pageUrlDelegate);

            htmlString.ToString().ShouldBe(@"<a href=""page1"">1</a><a class=""selected"" href=""page2"">2</a><a href=""page3"">3</a>");
        }
Beispiel #2
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++)
            {
                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());
            }

            return MvcHtmlString.Create(result.ToString());
        }