6

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

Marthijn
  • 3,292
  • 2
  • 31
  • 48
  • 1
    see this question - http://stackoverflow.com/questions/4710853/using-mvc-htmlhelper-extensions-from-razor-declarative-views – Graham Clark Oct 18 '11 at 13:54

2 Answers2

7

You could add the following line to your helper, to define Html

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

(Copied from this answer)

Community
  • 1
  • 1
GvS
  • 52,015
  • 16
  • 101
  • 139
2

Further to the accepted answer, to make @Html available throughout the 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;    
        }
   }
}