自定义HTML助手不起作用 [英] Custom Html Helper not working
本文介绍了自定义HTML助手不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从Pro ASP.NET MVC3框架学习MVC3.但是我被困在添加自定义HTML Helper的地方.
I am trying to learn MVC3 from Pro ASP.NET MVC3 Framework. But i am stuck at one place where we add custom Html Helper.
我做了书中提到的所有事情,但是我无法添加自定义的HTML帮助程序.
I did every thing mentioned in the book, but i am not able to add the custom Html helper.
有人可以帮忙吗?
谢谢
List.cshtml
@model SportsStore.WebUI.Models.ProductListViewModel
@{
ViewBag.Titke = "Product";
}
<!DOCTYPE html>
<html>
<head>
<title>List</title>
</head>
<body>
<div>
@foreach (var p in Model.Products)
{
<div class="item">
@p.Name
@p.Description
<h4>@p.Price.ToString("c")</h4>
</div>
}
<div class="Pager">
@Html.PageLinks(Model.pagingInfo, x => Url.Action("List", new {page = x}))
</div>
</div>
</body>
</html>
PagingHelper.Cs
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.WebUI.Models;
using System.Text;
namespace SportsStore.WebUI.HtmlHelpers
{
public static class PagingHelper
{
public static MvcHtmlString PageLinks(HtmlHelper helper, PagingInfo pagingInfo, Func<int, string> pageUrl)
{
StringBuilder linkString = 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");
}
linkString.Append(tag.ToString());
}
return MvcHtmlString.Create(linkString.ToString());
}
}
}
Web.Config
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="SportsStore.WebUI.HtmlHelpers" />
</namespaces>
</pages>
</system.web.webPages.razor>
推荐答案
您没有创建扩展方法.
要创建扩展方法,您需要使用this
关键字修饰第一个参数.
To make an extension method, you need to decorate the first parameter with the this
keyword.
这篇关于自定义HTML助手不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文