[英]ASP.Net MVC render view from another controller to string
I am struggling for hours to render a view from another controller to a string. 我努力工作几个小时才能将另一个控制器的视图呈现为字符串。 I use that to generate email templates.
我用它来生成电子邮件模板。 For this I created a controller called EmailController that has a view ConfirmationEmail.cshtml I want to render that view in Home.Index action for example.
为此,我创建了一个名为EmailController的控制器,该控制器具有一个视图ConfirmationEmail.cshtml,例如,我想在Home.Index操作中呈现该视图。 The reason for this is organisation because I want to have the email views in ~/View/Email/... and EmailController will be used just for rendering them.
这样做的原因是组织性,因为我想在〜/ View / Email / ...中使用电子邮件视图,而EmailController将仅用于呈现它们。 Also ~/View/Shared/Email/... will be ok if you have another suggestion.
如果您有其他建议,〜/ View / Shared / Email / ...也可以。
Do you suggest another approach? 您是否建议另一种方法?
I saw this thread ASP.NET MVC Razor: How to render a Razor Partial View's HTML inside the controller action but I cannot make it work. 我看到了该线程ASP.NET MVC Razor:如何在控制器动作中呈现Razor局部视图的HTML,但是我无法使其工作。
I need something like: 我需要类似的东西:
public ActionResult Index()
{
EmailController emailController = new EmailController();
ControllerContext context = new ControllerContext(this.ControllerContext.RequestContext, emailController);
EmailController.RenderPartialViewToString(emailController, "ConfirmationEmail", new EmailModel());
}
This does not work :( and the viewResult object in RenderPartialViewToString method is null. Do you have a solution for this? :(这不起作用,并且RenderPartialViewToString方法中的viewResult对象为null。您对此有解决方案吗?
Did you check out MvcMailer project? 您是否签出了MvcMailer项目?
https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide
Why not use FluentEmail ? 为什么不使用FluentEmail ? It takes a few seconds to setup using Nuget, and you get all the goodness of Razor templates, with a simple to use email tool.
使用Nuget进行设置需要几秒钟,并且使用简单易用的电子邮件工具即可获得Razor模板的所有优点。
EmailController.RenderPartialViewToString
you passed in emailController
instead of context
您在
emailController
而不是context
传递的EmailController.RenderPartialViewToString
Modify that line to something like: 将该行修改为:
EmailController.RenderPartialViewToString(context, "ConfirmationEmail", new EmailModel())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.