Rendering a View to a String – Email Templates in MVC4

Hey everyone,

This is just a short post showing how to render views as strings. I’ve used this as a quick way to generate email templates. The logic to render a view as a string is as follows:

public static string RenderRazorViewToString(Controller controller, string viewName, string content)
        {
            controller.ViewBag.Content = content;
 
            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
                var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);
 
                return sw.GetStringBuilder().ToString();
            }
        }

The Email Template: Views/Emails/DefaultTemplate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@{
    Layout = null;
}
 
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 
    <title>Easy Carts</title>
 
</head>
<body bgcolor='#f5f6f7' style='font-family: Arial, Helvetica, sans-serif;'>
 
 
    <!-- Wrapper -->
    <table width='100%' align='center' bgcolor='#f5f6f7' cellpadding='40' style='border-radius:3px;'>
        <tr>
            <td>
                <table width='500px;' align='center' bgcolor='#FFFFFF' cellpadding='15'>
                    <tr>
                        <td style='font-size:13px;' align='left'>                           
                            @ViewBag.Content                            
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr align='center'>
            <td align='center'>
                <a href="http://easycarts.com.au">
                    <img src='http://easycarts.com.au/Images/email-logo.png' />
                </a>
            </td>
        </tr>
    </table>
</body>
</html>

In your controller:

public String TestEmail(String content)
        {
            Email email = new Email() {
                To = "lls@live.com.au",
                From = Email.Fields.From.NoReply,
                Body = content,
                Subject = "Test",
                TemplateName = @"~\Views\Emails\DefaultTemplate.cshtml",
                UseTemplate = true
            };
 
            email.SendEmail(this);
 
            db.Emails.Add(email);
            db.SaveChanges();
 
            return email.Body.ToString();
        }

If you looking to implement this with your emails, note that it builds on top of the email class we created back in this post: http://www.whatibroke.com/?p=963

If you’re just going to be using it directly within the controller, using the original code from over StackOverflow will probably be your best bet: http://stackoverflow.com/a/2759898/522859

3 thoughts on “Rendering a View to a String – Email Templates in MVC4

  1. FOJ

    Can i please get the implemented email from you as i av been struggling and i’ve not been able to implement it

    Reply
    1. Chris Owens Post author

      Hey FOJ,

      Sorry I don’t have the code available anymore – what issues are you getting?

      Cheers,
      Chris

      Reply

Leave a Reply