I need a HtmlHelper instance in my controller.
How can I instantiate one? thanks
This does not build:
var h = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView("omg"), new ViewDataDictionary(), new TempDataDictionary()), new ViewPage());
Here is a screenshot of the error
Also, when I look at the list of methods under var h
, I only see my custom extension methods and no regular ones like ActionLink
. So that one needs to list as well. (solved by sternr)
Solution:
Ensure System.Web.Mvc.Html is included.
Here is the code to instantiate a HtmlHelper.
System.IO.TextWriter writer = new System.IO.StringWriter();
var html = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView(ControllerContext, "omg"), new ViewDataDictionary(), new TempDataDictionary(), writer), new ViewPage());