MVC extension method error

Hi I have 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 am calling extension method in 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" takes a first argument of type 'System.Web.Mvc.HtmlHelper' (unless you specified directive or assembly reference?)

I added a namespace to web.config inside the view 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 I could solve this problem.

+2


source to share


4 answers


Try adding

@using SportsStore.WebUI.HtmlHelpers;

      



to the beginning of your .cshtml file

your namespace approach should work as well, so try shutting down the server, rebuild your solution and start again.

+4


source


It looks like you added a namespace reference to your root web.config file.

If you are using MVC3 with the Razor view engine, you need to add the namespace reference to the Views \ web.config file. It will then be available globally for all views in 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


source


You seem to have done everything right. Have you tried compiling your web project before trying to use the Html helper?

0


source


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

@model SportsStore.WebUI.Models.ProductsListViewModel

      

0


source







All Articles