[英]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.