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.

1
  • Looks like your question was answered by SLaks - could you pelase mark it as such? Commented Aug 4, 2011 at 18:51

3 Answers 3

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(...)
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Yes; you should ask a separate question.
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... 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!
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.