asp.net-mvc - HTML 帮助器类方法不起作用

标签 asp.net-mvc asp.net-mvc-3 html-helper

我被 Steven Sanderson/Adum Freeman Pro ASP .Net MVC 3 的引用书困住了。我已经读到了第 185 页,其中使用 HTML 帮助程序返回链接中的页面编号。我在这个网站上找到了帮助解决我与这本引用书有关的问题的帮助,并且完成了仍然遇到相同问题的每个步骤(链接)MVC extension method error

当我在浏览器中运行代码时,出现此错误:

Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'PageLinks' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax

代码构建得很好,但如果我打开任何其他类来编辑这行代码到我的辅助方法,则会出现与上面相同的错误。

@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new {page = x}))

帮助类:

namespace SportsStore.WebUI.HtmlHelpers
{
    public static class PagingHelpers
    {
        public static MvcHtmlString PageLinks(this HtmlHelper html, 
                                                PagingInfo pagingInfo, 
                                                Func<int, string> pageURL)
        {
            StringBuilder results = 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");
                results.Append(tag.ToString());
            }
            return MvcHtmlString.Create(results.ToString());
        }
    }
}

我的观点:

@{
    ViewBag.Title = "Products";
 }

@foreach (var p in Model.Products) { 
    <div class="item">
        <h3>@p.Name</h3>
        @p.Description
        <h4>@p.Price.ToString("c")</h4>
    </div>
}

<div class="pager">
    @Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new {page = x}))
</div>

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>

最佳答案

您正在将动态值传递给扩展方法。 (将鼠标悬停在 Model.PagingInfo 上,智能感知应该告诉您该类型是动态的。这意味着它直到运行时才知道该类型是什么)因此,尝试更改您的代码,以便它像这样转换动态类型:

@Html.PageLinks((PagingInfo)Model.PagingInfo, x => Url.Action("List", new {page = x}))

您可以通过其他两种方式解决此问题:

正如错误所示,不要使用扩展方法调用它:

PageLinks(Html, Model.PagingInfo, x => Url.Action("List", new {page = x}))

或者您可以通过将其设置在 View 顶部来让 View 知道模型将是什么,这样它就不会使用动态

@model PagingInfo

关于asp.net-mvc - HTML 帮助器类方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798115/

相关文章:

c# - 如何在 ASP.NET WebAPI 中返回文件 (FileContentResult)

asp.net-mvc - Asp.net MVC 3中的自定义成员资格提供程序

asp.net-mvc - MVC 中 SelectlistItem 的自定义属性

asp.net-mvc - 是否有用于图像链接的 ASP.NET MVC HtmlHelper?

asp.net-mvc-3 - ASP.Net MVC 3 - 使用 DataAnnotations 的不显眼的客户端验证 - 字段是必需的,但不应该是

c# - 在区域内的 View 中使用 MVC HtmlHelper 扩展方法

C# 6.0 功能不适用于 Visual Studio 2015

javascript - flow.js 点击上传文件

asp.net-mvc - 如何让 AutoMapper 填充我的 View 模型?

c# - 在安装了 MVC 3 的系统中,我在哪里可以找到 System.Web.MVC dll?