Email PartialView Content

I have a PartialView that contains HTML code with Razor annotations. It generates a page for me that I want to send an email to someone. Is there a way to translate this PartialView into HTML content to send it?

+5
source share
1 answer

I would suggest using MvcMailer , which does exactly what you want (without having to write code for it .. it can also do this asynchronously):

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

Update

As stated in the comments, a solution for its implementation (I still think that MvcMailer will simplify your life):

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: HTML- Razor Partial View )

+5

All Articles