Html.ActionLink cannot be used in Razor global helper

I created a global .cshtml Razor file in the App_Code folder of my MVC project to declare @helper functions. The problem is that I cannot use Html.ActionLink (or other extensions) in helper functions. I tried importing classes via @using, but that didn't work. Any ideas?

+6
source share
2 answers

You can add the following line to your helper to define Html

  var Html = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html; 

(Copied from this answer)

+7
source

In addition to the accepted answer, to make @Html available in the entire helper file:

 @using System.Web.Mvc.Html ... @functions { protected static new System.Web.Mvc.HtmlHelper Html { get { return ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html; } } } 
+2
source

Source: https://habr.com/ru/post/899528/


All Articles