views:

28

answers:

1

Hello

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

+1  A: 

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.

mxmissile
Is there some easy way of modifying my code instead of adding mvccontrib? something.renderview?
molgan