Send asp.net mvc action result inside email

前端 未结 5 2090
礼貌的吻别
礼貌的吻别 2020-12-01 02:04

I\'d like to use my Action in asp.net mvc, as template engine, that results in form of string, that I could send in email.

Pseudo-Code:

public Action         


        
5条回答
  •  没有蜡笔的小新
    2020-12-01 03:00

    http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/ has a good solution for rendering a View to a string so you can send it in email. He notes "rendering a partial view to string has, thankfully, become much, much easier."

    /// Static Method to render string - put somewhere of your choosing
    public static string RenderPartialToString(string controlName, object viewData)
    {
         ViewDataDictionary vd = new ViewDataDictionary(viewData);
         ViewPage vp = new ViewPage { ViewData = vd };
         Control control = vp.LoadControl(controlName);
    
         vp.Controls.Add(control);
    
         StringBuilder sb = new StringBuilder();
         using (StringWriter sw = new StringWriter(sb))
         {
             using (HtmlTextWriter tw = new HtmlTextWriter(sw))
             {
                 vp.RenderControl(tw);
             }
         }
    
         return sb.ToString();
    }
    

提交回复
热议问题