Create an instance of HtmlHelper in the controller

I need to do with the HtmlHelper in the controller, so how do I create it in the controller (asp.net mvc 2.0)?

+3
source share
2 answers

Is this what you want?

Using HtmlHelper in the controller

EDIT

Use it;

System.IO.TextWriter writer = new System.IO.StringWriter();

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

string g = h.TextBox("myname").ToString();
+8
source

You can use this method:

public static HtmlHelper GetHtmlHelper(this Controller controller)
{
 var viewContext = new ViewContext(controller.ControllerContext, new FakeView(), controller.ViewData, controller.TempData, TextWriter.Null);
 return new HtmlHelper(viewContext, new ViewPage());
}

public class FakeView : IView
{
 public void Render(ViewContext viewContext, TextWriter writer)
 {
  throw new NotSupportedException();
 }
}
+7
source

Source: https://habr.com/ru/post/1728869/


All Articles