33

Where can I find tutorials on how exactly get this to work?

I am trying to have @helper in App_code directory of my MVC3 app, so far when I try to use them in other razor pages, they cannot be found.

Ray
  • 12,101
  • 27
  • 95
  • 137

3 Answers3

51

You need to call the helpers as static methods of the page name.

For example, if you have a helper named HelpMe in App_Code/MyHelpers.cshtml, you would write

@MyHelpers.HelpMe(...)
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    It might not be the "Areas" that are the problem. I can use the @MyHelper.Help() syntax just fine in a normal ASP.NET MVC project. But I'm trying to get it working in an Orchard module, implemented as a separate project. And in that separate project, which isn't itself a full Web app, the @MyHelper.Help() syntax works in the IDE, but at runtime it says, "Compiler Error Message: CS0103: The name 'MyHelper' does not exist in the current context." It doesn't seem to be picking up the files in the App_Code directory for compiling. Might be worth its own question. – Ken Smith Jun 23 '11 at 16:52
  • 1
    Yes; you should ask a separate question. – SLaks Jun 23 '11 at 16:54
  • Hi! I have created an idea in ASP.NET to make this more useful: [Support @helper ExtensionMethod(this HtmlHelper html) for views in APP_CODE](http://aspnet.uservoice.com/forums/41201-asp-net-mvc/suggestions/3670180-support-helper-extensionmethod-this-htmlhelper-ht)... I know this is just a little bit of suggar, but it could make a lot for people trying to write html extension methods... Thanks! – Miguel Angelo Feb 20 '13 at 02:29
8

Scott Gu just wrote a blog post about this:

http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx

Dimskiy
  • 5,233
  • 13
  • 47
  • 66
1

Look at this SO question and associated answer: Razor: Declarative HTML helpers

It does work, though I find that in the end, I usually opt for writing another extension method off of HtmlHelper.

Community
  • 1
  • 1
Brian Ball
  • 12,268
  • 3
  • 40
  • 51