HTML helper class method not working

江枫思渺然 提交于 2019-12-10 10:06:34

问题


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:

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}))

Helper Class:

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());
        }
    }
}

My View:

@{
    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>

回答1:


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



回答2:


you must add this lines in web.cofig under Views Folder not main one

  <add namespace="YourProjectName.HtmlHelpers"/>



回答3:


Add @using project_name.HtmlHelpers e.g.

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



来源:https://stackoverflow.com/questions/9798115/html-helper-class-method-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!