asp.net-mvc-3 - MVC 扩展方法错误

标签 asp.net-mvc-3 c#-4.0 extension-methods

您好,我的 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/

相关文章:

c# - 如何将参数传递给 jQuery document.ready() 函数(ASP.NET MVC、C#)

c# - 验证不使用模型时是否选中复选框

asp.net-mvc-3 - byte []到ASP.NET MVC网站中的音频

c# - 如何绕过 Marshal.Copy(32 位)长度限制?

c# - 实例化扩展方法? C#

arrays - 扩展数组的数组 - Swift 4.1

jQuery datetimepicker 太多递归错误

c# - 为什么使用 SqlParameter[] 而不是嵌入参数?

.net - Google 身份验证 [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)] 返回 UNAUTHORIZED

C# - 聚合中的终止()