0

I have this function:

public static string RenderViewToString(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();

}

And I call it like this:

string body = StringHelpers.RenderViewToString("~/Areas/Public/Views/Shared/RegistrationEmail.ascx", new RegistrationEmailViewModel { User = user });

And it returns a html-table with the user-info.

But I was wondering if there is a way to edit this to I can can return a View as string? so I can add masterpage, so it'll be easier to design all potential mails going out?

Thanks in advance /M

Lasse Edsvik
  • 9,070
  • 16
  • 73
  • 109

1 Answers1

1

Check out MVCContrib's email template system for sending emails.

http://codevanced.net/post/Sending-HTML-emails-with-ASPNET-MVC2-and-MVCContrib.aspx

Update:

This question and/or this article might help if you don't want to include Mvccontrib. Although I use Mvccontrib every day, it's harmless.

Community
  • 1
  • 1
mxmissile
  • 11,464
  • 3
  • 53
  • 79