5

Possible Duplicate:
Using @helpers from another View in Razor ASP.Net MVC3

My environment: ASP.NET MVC3 with Razor.

I have some reusable view logic in declarative helpers in the App_Data directory, which are defined as @helper bar() { /* markup */ } and are public for any view to use.

Also, in a regular Razor view I can write "private" helpers as such:

@functions {
  public IHtmlString foo() { return new MvcHtmlString("foo"); }
}

I would like to have "private" functions within my declarative helpers, OR, I would like to have "private" declarative helpers within my declarative helper files. The alternative is to write regular helper classes--but I prefer markup in this case as it's less verbose and easier to maintain.

Why would you want to do this? You have a view, and you want to pull in some markup; that's done by referencing a declarative helper. But that DH is in a file full of other DHs which use common markup. So you want to move all that commonality into a single place: a "private" declarative function/helper which only the DHs in that file can see.

Community
  • 1
  • 1
Peter Marks
  • 989
  • 2
  • 8
  • 15
  • Not the same as the other question. This one explains why one would want to take this approach--the rationale for wanting such a feature. It is incidental that it has the same conclusion. – Peter Marks Nov 30 '11 at 05:54
  • This is not the same as the other question, as the other question is looking for helpers only accessible to some views besides the declarative file (just not to all views), while this one looks for helpers that are private to the declarative helpers only (in other words helpers or functions local to the file, just as ina regular view). – yoel halb Jun 06 '13 at 15:53
  • Have you actually tested using private functions in the App_Code folder? according to http://stackoverflow.com/questions/4760783/is-it-possible-to-create-a-generic-helper-method-with-razor/5285842#5285842 public functions should work in the App_Code folder, I have not tested private yet, but it appears as it should work. – yoel halb Jun 06 '13 at 16:05

0 Answers0