Using HtmlHelper in a Controller

后端 未结 7 1433
悲哀的现实
悲哀的现实 2020-11-27 04:34

Is it possible to use HtmlHelper in a controller, for-example to get the TextBox(...) method? not that I can\'t write the html that it generates myself, but I just want to u

相关标签:
7条回答
  • 2020-11-27 05:21

    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 InvalidOperationException();
        }
    }
    
    0 讨论(0)
提交回复
热议问题