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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.