asp.net-mvc 在 MVC3 Razor 中,如何在操作中获取渲染视图的 html?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4692131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:43:56  来源:igfitidea点击:

In MVC3 Razor, how do I get the html of a rendered view inside an action?

asp.net-mvcasp.net-mvc-3razor

提问by Omu

Does anybody know how to get the generated html of a view inside an action?

有人知道如何在动作中获取视图的生成 html 吗?

Is it something like this:

是不是像这样:

public ActionResult Do()
{
    var html = RenderView("hello", model);
...
}

回答by Chev

I use a static method in a class I called Utilities.CommonI pass views back to the client as properties of JSON objects constantly so I had a need to render them to a string. Here ya go:

我在我调用的类中使用静态方法Utilities.Common我不断将视图作为 JSON 对象的属性传递回客户端,因此我需要将它们呈现为字符串。给你:

public static string RenderPartialViewToString(Controller controller, string viewName, object model)
{
    controller.ViewData.Model = model;
    using (StringWriter sw = new StringWriter())
    {
        ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
        ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
        viewResult.View.Render(viewContext, sw);

        return sw.ToString();
    }
}

This will work for full views as well as partial views, just change ViewEngines.Engines.FindPartialViewto ViewEngines.Engines.FindView.

这适用于完整视图和部分视图,只需更改ViewEngines.Engines.FindPartialViewViewEngines.Engines.FindView.

回答by Gary McGill

The accepted answer by @Chev above is good, but I wanted to render the result of a specific action, not just a particular view.

上面@Chev 接受的答案很好,但我想呈现特定操作的结果,而不仅仅是特定视图

Also, I needed to be able to pass parameters to that action rather than rely on injecting a model.

此外,我需要能够将参数传递给该操作,而不是依赖于注入模型。

So I came up with my own method, that I put in the base class of my controllers (making it available to them all):

所以我想出了我自己的方法,我把它放在我的控制器的基类中(使其可供所有人使用):

    protected string RenderViewResultAsString(ViewResult viewResult)
    {
        using (var stringWriter = new StringWriter())
        {
            this.RenderViewResult(viewResult, stringWriter);

            return stringWriter.ToString();
        }
    }

    protected void RenderViewResult(ViewResult viewResult, TextWriter textWriter)
    {
        var viewEngineResult = this.ViewEngineCollection.FindView(
            this.ControllerContext, 
            viewResult.ViewName, 
            viewResult.MasterName);
        var view = viewEngineResult.View;

        try
        {
            var viewContext = new ViewContext(
                this.ControllerContext, 
                view, 
                this.ViewData, 
                this.TempData, 
                textWriter);

            view.Render(viewContext, textWriter);
        }
        finally
        {
            viewEngineResult.ViewEngine.ReleaseView(this.ControllerContext, view);
        }
    }

Suppose I have an action called Foothat takes a model object and some other parameters, which together influence what view will be used:

假设我有一个名为的动作Foo,它接受一个模型对象和一些其他参数,它们共同影响将使用的视图:

    public ViewResult Foo(MyModel model, int bar)
    {
        if (bar == 1)
            return this.View("Bar1");
        else
            return this.View("Bar2", model);
    }

Now, if I want to get the result of calling action Foo, I can simply get the ViewResultby invoking the Foomethod, and then call RenderViewResultAsStringto get the HTML text:

现在,如果我想获取调用 action 的结果Foo,我可以简单地ViewResult通过调用Foo方法获取,然后调用RenderViewResultAsString获取 HTML 文本:

    var viewResult = this.Foo(model, bar);

    var html = this.RenderViewResultAsString(viewResult);