我想访问一个类中的Html
对象,因此我可以使用Class.Method
调用Html.Partial
来呈现内容。
有没有办法可以称之为Class.Method()
而不是Class.Method(Html)
?
答案 0 :(得分:1)
您可能需要引用HtmlHelper
来调用Partial
方法。
您可以将其作为HtmlHelper
public static MvcHtmlString Method(this HtmlHelper helper, Class @class)
{
return helper.Partial(....);
}
或者在HtmlHelper
上的Method
方法中创建一个Class
,这可能会更成问题,因为除非您通过它找到对该类的引用,否则该类将不存在你的HttpContext
您可以在控制器上轻松创建HtmlHelper,如下所示:
HtmlHelper _htmlHelper;
public HtmlHelper HtmlHelper
{
get
{
if (_htmlHelper == null)
{
TextWriter writer = new StringWriter();
_htmlHelper = new HtmlHelper(new ViewContext(ControllerContext,
new WebFormView("Default"),
new ViewDataDictionary(),
new TempDataDictionary(), writer), new ViewPage());
}
return _htmlHelper;
}
}