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.
source to share
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>
source to share