5

I would like to create a static helper method that I can call from a view.

Is it possible for a helper method to have access to the current ViewContext without needing to explicitly pass the ViewContext to the method as a parameter?

Something like HttpContext.Current except for ViewContext.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
Keith
  • 2,618
  • 4
  • 29
  • 44

1 Answers1

8
public static class XTenshuns
{
    public static string MyHtmlHelper(this HtmlHelper helper)
    {
        // it's right here -> helper.ViewContext
    }
}
pdr
  • 6,372
  • 1
  • 29
  • 38
  • 3
    Can I only do it via extension methods then? What if I wanted a plain ol' helper method? – Keith Feb 19 '10 at 01:11
  • Well, it couldn't be a plain old helper method. If you wanted to make it more lightweight, you could rip off some of the code from HtmlHelper to roll something similar... even then you'd need to add that as a property somewhere, probably a custom ViewPage. Nah, it's been written this way for a reason. Why so against HtmlHelper? – pdr Feb 19 '10 at 01:36