Create HtmlHelper instance in Controller

前端 未结 2 1185
再見小時候
再見小時候 2021-01-20 18:05

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

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-20 18:49

    You can use method like this:

    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();
     }
    }
    

提交回复
热议问题