簡體   English   中英

將PartialView內容作為電子郵件發送

[英]Send PartialView content as email

我有一個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:如何在控制器動作中呈現Razor Partial View的HTML

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM