Reputation: 100331
I would like to access the Html
object inside a class, so I could call Class.Method
to render something using Html.Partial
.
Is there a way I can call it Class.Method()
instead of Class.Method(Html)
?
Upvotes: 1
Views: 62
Reputation: 63522
One way or another you're going to need a reference to HtmlHelper
to call the Partial
method.
You can make it an extension of HtmlHelper
public static MvcHtmlString Method(this HtmlHelper helper, Class @class)
{
return helper.Partial(....);
}
Or create an HtmlHelper
from within the Method
method on Class
, which might be more problematic since the Context won't exist on that class unless you find a reference to it through your HttpContext
You can easily create an HtmlHelper on your Controller like so:
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;
}
}
Upvotes: 1