Search code examples
asp.net-mvcasp.net-mvc-5html-helper

Referencing @Html.Actionlink in a custom helper


I have a custom helper which needs to call Html.ActionLink

@helper GetLink(int id, string name)
{
    @Html.ActionLink(name, "Index", "MyController", new { id = id }, null);
}

It works perfectly on another page but within my custom helper (Called customhelpers.cshtml and located within the App_Code folder) I get the following error:

'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'ActionLink' and the best extension method overload 'System.Web.Mvc.Html.LinkExtensions.ActionLink(System.Web.Mvc.HtmlHelper, string, string, string, object, object)' has some invalid arguments

This means it cannot find the Html.ActionLink class however I tried referencing the relevant namespace (@using System.Web.MVC.Html) but to no avail.


Solution

  • Htmlhelpers are not accessible from the App_Code, but you can see a nice workaround here from Paul Stovell (also mentioned by ScottGu)