HTML helper method not working

I got stuck in Stephen Sanderson's / Adum Freeman Pro ASP.Net MVC 3 reference. I've got it to page 185 where the HTML helper should be used to return the number of pages in links. I found help on this site referring to my problem with this reference and went through each step while still having the same problems (link) MVC extension method error

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

Compiler error message: CS1973: 'System.Web.Mvc.HtmlHelper' does not have an applicable method named "PageLinks", but it seems to have an extension method using that name. Extension methods cannot be dynamically dispatched. Think about using dynamic arguments or calling an extension method without the extension method syntax

The code builds fine, but if I open any other class to edit this line of code, 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>

      

+3


source to share


2 answers


You are passing a dynamic value to the extension method. (Hover over Model.PagingInfo and intellisense, tell you the type is dynamic. This means it doesn't know what the type is until it is executed). So, try changing your code to give out a dynamic type like this:

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

      

You can fix this in two other ways:

As this error suggests, don't call it with an extension method:



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

      

OR you could make the view known what will happen to the model so that it doesn't use dynamics by setting it at the top of your view

@model PagingInfo

      

+3


source


you have to add these lines to web.cofig under Views not main



  <add namespace="YourProjectName.HtmlHelpers"/>

      

0


source







All Articles