From 2ddc93b2a992ef60d49b379fedfb037c8d9a48c8 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Sat, 21 Aug 2021 17:03:09 +1000 Subject: [PATCH] 7.5.1.2 Adding the HTML Helper Method; e.g. 7.17 The PagingHelpers Class --- .../HtmlHelpers/PagingHelpers.cs | 33 +++++++++++++++++++ .../SportsStore.WebUI.csproj | 1 + 2 files changed, 34 insertions(+) create mode 100644 SportsStore/SportsStore.WebUI/HtmlHelpers/PagingHelpers.cs diff --git a/SportsStore/SportsStore.WebUI/HtmlHelpers/PagingHelpers.cs b/SportsStore/SportsStore.WebUI/HtmlHelpers/PagingHelpers.cs new file mode 100644 index 0000000..e8b323d --- /dev/null +++ b/SportsStore/SportsStore.WebUI/HtmlHelpers/PagingHelpers.cs @@ -0,0 +1,33 @@ +using SportsStore.WebUI.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Web; +using System.Web.Mvc; + +namespace SportsStore.WebUI.HtmlHelpers +{ + public static class PagingHelpers + { + public static MvcHtmlString PageLinks(this HtmlHelper html, + PagingInfo pagingInfo, + Func pageUrl) + { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < pagingInfo.TotalPages; i++) + { + TagBuilder tag = new TagBuilder("a"); // Construct an 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()); + } + } +} \ No newline at end of file diff --git a/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj b/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj index e7e90fe..f38f303 100644 --- a/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj +++ b/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj @@ -164,6 +164,7 @@ Global.asax +