7.5.1.2 Adding the HTML Helper Method; e.g. 7.17 The PagingHelpers Class

chap07
Jason Zhu 2021-08-21 17:03:09 +10:00
parent 103a66938d
commit 2ddc93b2a9
2 changed files with 34 additions and 0 deletions

View File

@ -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<int, string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < pagingInfo.TotalPages; i++)
{
TagBuilder 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());
}
}
}

View File

@ -164,6 +164,7 @@
<Compile Include="Global.asax.cs"> <Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon> <DependentUpon>Global.asax</DependentUpon>
</Compile> </Compile>
<Compile Include="HtmlHelpers\PagingHelpers.cs" />
<Compile Include="Infrastructure\NinjectControllerFactory.cs" /> <Compile Include="Infrastructure\NinjectControllerFactory.cs" />
<Compile Include="Models\PagingInfo.cs" /> <Compile Include="Models\PagingInfo.cs" />
<Compile Include="Product.cs"> <Compile Include="Product.cs">