MVC extension method error

2019-07-17 15:42发布

问题:

Hi i've got an extension method in my PagingHelpers class:

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

here i call extension method in the List.cshtml:

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

And I got this error:

'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?)

I added the namespace in the web.config inside the Views Folder:

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

Please help me, I don't know how could I solve this problem

回答1:

Try adding

@using SportsStore.WebUI.HtmlHelpers;

to the top of your .cshtml file

your namespace approach should work as well, so try to shut down the server rebuild your solution and run again



回答2:

It appears you have added the namespace reference to the root web.config file.

If you are using MVC3 with the Razor view engine, you have to add the namespace reference to the Views\web.config file. Then it will be globally available to all views within the View folder.

<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:

It seems you did everything correctly. Did you compile your web project before trying to use the Html helper?



回答4:

You need to add in your List.cshtml above the code this line

@model SportsStore.WebUI.Models.ProductsListViewModel