您好,我的 PagingHelpers 类中有一个扩展方法:
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 = 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());
}
}
}
这里我调用了List.cshtml中的扩展方法:
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new {page = x}))
我得到了这个错误:
'System.Web.Mvc.HtmlHelper' does not contain a definition for 'PageLinks' and no extension method 'PageLinks' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
我在 Views 文件夹内的 web.config 中添加了命名空间:
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
<add namespace="SportsStore.WebUI.HtmlHelpers"/>**
</namespaces>
</pages>
请帮帮我,我不知道怎么解决这个问题
最佳答案
尝试添加
@using SportsStore.WebUI.HtmlHelpers;
到您的 .cshtml 文件的顶部
您的命名空间方法也应该有效,因此请尝试关闭服务器重建您的解决方案并再次运行
关于asp.net-mvc-3 - MVC 扩展方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8639214/