简体   繁体   English

ASP.NET Core 将包含部分视图的视图渲染为字符串

[英]ASP.NET Core Render a View with Partial Views inside to String

I am struggling to render a view that has a partial inside the view.我正在努力渲染一个在视图内部有部分的视图。

The View does get rendered to string, but the partial inside the view does not get rendered at all.视图确实被渲染为字符串,但视图内部的部分根本没有被渲染。

Here is my View to String Utility Code:这是我对字符串实用程序代码的看法:

public static class EmailUtility
    {
        public static async Task<string> RenderPartialViewToString(this Controller controller, string viewName, object model)
        {
            controller.ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                IViewEngine viewEngine = controller.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
                ViewEngineResult viewEngineResult = GetViewEngineResult(controller, viewName, false, viewEngine);

                if (!viewEngineResult.Success)
                {
                    return $"A view with the name {viewName} could not be found";
                }

                ViewContext viewContext = new ViewContext(
                    controller.ControllerContext,
                    viewEngineResult.View,
                    controller.ViewData,
                    controller.TempData,
                    sw,
                    new HtmlHelperOptions()
                );

                await viewEngineResult.View.RenderAsync(viewContext);

                return sw.GetStringBuilder().ToString();
            }
        }

        private static ViewEngineResult GetViewEngineResult(Controller controller, string viewName, bool isPartial, IViewEngine viewEngine)
        {
            if (viewName.StartsWith("~/"))
            {
                var hostingEnv = controller.HttpContext.RequestServices.GetService(typeof(IWebHostEnvironment)) as IWebHostEnvironment;
                return viewEngine.GetView(hostingEnv.WebRootPath, viewName, !isPartial);
            }
            else
            {
                return viewEngine.FindView(controller.ControllerContext, viewName, !isPartial);
            }
        }
    }

This is how I call the method in the Controller:这就是我在控制器中调用方法的方式:

string html = await this.RenderPartialViewToString("~/Views/Shared/MailerLayoutMaster.cshtml", model);

This is the MailerLayoutMaster.cshtml:这是 MailerLayoutMaster.cshtml:

@model Model

@{ 
    Layout = null;
}

<!DOCTYPE html>
<html>
    <head>

    </head>
    <body>
        @{ 
            switch ((int) Model.EmailType)
            {
                case (int) EmailTypeEnum.ContactUsSubmitted:
                    await Html.PartialAsync("../Shared/MailTemplates/EmailPartial.cshtml", Model);
                    break;
                default:
                    
                    break;
            }
        }
    </body>
</html>

And this is my EmailPartial.cshtml这是我的 EmailPartial.cshtml

@model Model

<table>
    <tr>
        <td>Name: <b>@Model.Name</b></td>
    </tr>
    <tr>
        <td>Surname: <b>@Model.Surname</b></td>
    </tr>
    <tr>
        <td>Email: <b>@Model.Email</b></td>
    </tr>
    <tr>
        <td>Number: <b>@Model.Phone</b></td>
    </tr>
</table>

This is the string I get:这是我得到的字符串:

<!DOCTYPE html>
<html lang="en-ZA">
    <head>

    </head>
    <body>

    </body>
</html>

Here is my project structure if it helps:如果有帮助,这是我的项目结构:

Controllers
    Controller.cs
Utilities
    ViewToStringUtil.cs
Views
    Shared
        MailTemplates
            EmailPartial.cshtml
        MailerLayoutMaster.cshtml

I think it's happened because you didn't call partial view the right way.no need to use .cshtml extension for call partial view.我认为这是因为您没有以正确的方式调用局部视图。不需要使用.cshtml扩展名来调用局部视图。 update your code like below:-更新您的代码,如下所示:-

string html = await this.RenderPartialViewToString("~/Views/Shared/MailerLayoutMaster", model);

And

 switch ((int) Model.EmailType)
            {
                case (int) EmailTypeEnum.ContactUsSubmitted:
                    await Html.PartialAsync("../Shared/MailTemplates/EmailPartial", Model);
                    break;

//clarify code

Also, you can try without full path as per your folder structure, just using a partial view name.此外,您可以根据文件夹结构尝试不使用完整路径,只需使用部分视图名称。 You can check it works or not this process.您可以在此过程中检查它是否有效。

string html = await this.RenderPartialViewToString("PartialViewName", model);

The solution was really simple.解决方案非常简单。

All I needed was a @ before我需要的只是一个@之前

await Html.PartialAsync("../Shared/MailTemplates/EmailPartial.cshtml", Model);

It should have been:本来应该是:

@await Html.PartialAsync("../Shared/MailTemplates/EmailPartial.cshtml", Model);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET Core 2.2 中将局部视图渲染为 HTML 字符串 - Render Partial View to HTML string in ASP.NET Core 2.2 当部分视图包含渲染操作时,将ASP.Net MVC部分视图渲染为字符串 - Render an ASP.Net MVC partial view to a string when the partial view contains a render action ASP.NET CORE 将 Razor 视图渲染为带有控制器参数的字符串 - ASP.NET CORE Render Razor View to string with controller parameters 如何在 ASP.NET CORE 1.1 中将视图呈现为字符串 - How to render View as String in ASP.NET CORE 1.1 在 ASP.NET Core 中将 Razor 视图渲染为字符串 - Render Razor View to string in ASP.NET Core ASP.NET Core 5 MVC 从视图中渲染字符串 - ASP.NET Core 5 MVC Render string from action in view 如何在一个视图中使用其视图模型调用多个局部视图 - ASP.NET MVC - How to call multiple partial views with their View Models inside a view - ASP.NET MVC 如何将位于应用程序部分 (DLL) 中的部分视图呈现到主 asp.net 核心 2.2 项目中 - How to render a partial view located in an application part (DLL) into main asp.net core 2.2 project ASP.Net Core Razor视图中的查询字符串 - Query string inside ASP.Net Core Razor view Asp.net核心Sakura.AspNetCore.PagedList和部分视图 - Asp.net core Sakura.AspNetCore.PagedList and partial views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM