HTML助手类方法不起作用 [英] HTML helper class method not working

查看:103
本文介绍了HTML助手类方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了Steven Sanderson/Adum Freeman Pro ASP .Net MVC 3的参考书中.我已经写到了第185页,其中使用了HTML帮助器来返回链接中的页面编号.我在此站点上找到了帮助,以解决本参考书中的问题,并逐步完成了仍然存在相同问题的每个步骤(链接)

I’m stuck in a reference book by Steven Sanderson/Adum Freeman Pro ASP .Net MVC 3. I’ve made it up to page 185 where a HTML helper is to be used to return the numberer of pages in links. I found help on this site addressing my issue with this reference book, and walked through every step still having the same issues (link) MVC extension method error

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

When I run the code in a browser I get this error:

编译器错误消息:CS1973:"System.Web.Mvc.HtmlHelper" 没有名为"PageLinks"的适用方法,但似乎有一个 该名称的扩展方法.扩展方法不能动态 派遣.考虑强制转换动态参数或调用 没有扩展方法语法的扩展方法

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

代码构建良好,但是如果我打开任何其他类来将这行代码编辑到我的辅助方法中,则会得到与上述相同的错误.

The code builds fine but if I open any other class to edit this line of code to my helper method gets the same error as above.

@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

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和intellisense上会告诉您该类型是动态的.这意味着直到运行时它才知道该类型是什么.)因此,请尝试更改代码,以便它像这样强制转换动态类型:

You are passing a dynamic value to an extension method. (Hover over Model.PagingInfo and intellisense should tell you that the type is dynamic. This means it does not know what the type is until runtime) So, try changing your code so that it casts the dynamic type like this:

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

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

You could fix this in two other ways:

错误提示,请勿使用扩展方法调用它:

As the error suggests, do not call it using the extension method:

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

或者您可以通过在视图顶部设置

OR you could make the view know what the model is going to be so that it does not use a dynamic, by setting this at the top of your view

@model PagingInfo

这篇关于HTML助手类方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆