HtmlHelper in the controller

I need an instance of HtmlHelper in my controller.

How can I create an instance? thanks

This is not built:

var h = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView("omg"), new ViewDataDictionary(), new TempDataDictionary()), new ViewPage()); 

Here is a screenshot of the error enter image description here

Also, when I look at the list of methods in var h , I see only my own extension methods and not the usual ones like ActionLink . So you also need to list. (solved by sternr)

Decision:

  • Make sure System.Web.Mvc.Html is enabled.

  • Here is the code for instantiating the 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());

+4
source share
1 answer

HtmlHelper.ActionLink and most of the methods you are probably looking for are extension methods declared in the System.Web.Mvc.Html .

As for creating an instance \ using HtmlHelper inside your controller, this is bad practice as you clearly combine the interface code with the controller code. What are you trying to achieve?

+2
source

All Articles