Marthijn
Marthijn

Reputation: 3402

Html.ActionLink cannot be used in global Razor helper

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?

Upvotes: 6

Views: 1951

Answers (2)

user1023602
user1023602

Reputation:

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;    
        }
   }
}

Upvotes: 2

GvS
GvS

Reputation: 52501

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)

Upvotes: 7

Related Questions