【问题标题】:Send PartialView content as email以电子邮件形式发送 PartialView 内容
【发布时间】:2013-04-02 18:34:25
【问题描述】:

我有一个包含带有 Razor 注释的 HTML 代码的 PartialView。它为我生成了一个我想通过电子邮件发送给任何人的页面。有没有办法把这个 PartialView 翻译成 HTML 内容来发送?

【问题讨论】:

标签: c# asp.net-mvc-3 sendmail asp.net-mvc-partialview


【解决方案1】:

我建议使用 MvcMailer 来做你想做的事(你不必为它写代码。它也可以异步完成):

https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide

更新

正如在 cmets 上所说,自己实现它的解决方案(我仍然认为 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)

【讨论】:

    猜你喜欢
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多