Search code examples
asp.netasp.net-mvcviewcontext

ASP.NET MVC - Get ViewContext from helper method


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.


Solution

  • public static class XTenshuns
    {
        public static string MyHtmlHelper(this HtmlHelper helper)
        {
            // it's right here -> helper.ViewContext
        }
    }