Get html from MVC 4 view into a string

后端 未结 3 1403
醉酒成梦
醉酒成梦 2020-12-08 05:12

I am trying to use the accepted answer from this question.

It seems that it will be exactly what i am looking for, but i have a problem. I don\'t know how to actuall

3条回答
  •  悲哀的现实
    2020-12-08 05:53

    This is pretty much a copy of dav_i's post except that you have a model with strong typing and also the ability of producing partial views:

    Rather than inherit Controller which means you have to remember to implement this every time, or inherit from a CustomControllerBase, which means you have to remember to inherit every time - simply make an extension method:

    public static class ControllerExtensions
    {
        public static string RenderView(this Controller controller, string viewName, TModel model, bool partial = false)
        {
            var controllerContext = controller.ControllerContext;
            controllerContext.Controller.ViewData.Model = model;
    
            // To be or not to be (partial)
            var viewResult = partial ? ViewEngines.Engines.FindPartialView(controllerContext, viewName) : ViewEngines.Engines.FindView(controllerContext, viewName, null);
    
            StringWriter stringWriter;
    
            using (stringWriter = new StringWriter())
            {
                var viewContext = new ViewContext(
                    controllerContext,
                    viewResult.View,
                    controllerContext.Controller.ViewData,
                    controllerContext.Controller.TempData,
                    stringWriter);
    
                viewResult.View.Render(viewContext, stringWriter);
                viewResult.ViewEngine.ReleaseView(controllerContext, viewResult.View);
            }
    
            return stringWriter.ToString();
        }
    }
    

    Then within your Controller you can call like this (for a full view):

    this.RenderView("ViewName", model);
    

    That means you will get the doctype and the HTML element etc too. For partial view use:

    this.RenderView("ViewName", model, true);
    

提交回复
热议问题