我有一个 PartialView,其中包含带有 Razor 注释的 HTML 代码。它为我生成一个页面,我想通过电子邮件将其发送给任何人。有没有办法把这个PartialView翻译成HTML内容发送出去?
最佳答案
我建议使用 MvcMailer,它可以完全满足您的需求(无需为其编写代码……它也可以异步执行):
https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide
更新
如评论中所述,自己实现的解决方案(我仍然认为 MvcMailer 会让您的生活更轻松):
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter()) {
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
( ASP.NET MVC Razor: How to render a Razor Partial View's HTML inside the controller action )
关于c# - 将 PartialView 内容作为电子邮件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15933972/