ASP.NET MVC - 从helper方法获取ViewContext

Kei*_*ith 5 asp.net asp.net-mvc viewcontext

我想创建一个静态帮助方法,我可以从视图中调用.

辅助方法是否可以访问当前的ViewContext而无需将ViewContext作为参数显式传递给方法?

类似于HttpContext.Current,除了ViewContext.

pdr*_*pdr 8

public static class XTenshuns
{
    public static string MyHtmlHelper(this HtmlHelper helper)
    {
        // it's right here -> helper.ViewContext
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我只能通过扩展方法来做吗?如果我想要一个简单的帮助方法怎么办? (3认同)