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

In your controller:

public String TestEmail(String content)
            Email email = new Email() {
                To = "",
                From = Email.Fields.From.NoReply,
                Body = content,
                Subject = "Test",
                TemplateName = @"~\Views\Emails\DefaultTemplate.cshtml",
                UseTemplate = true
            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:

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:

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

    1. Chris Owens Post author

      Hey FOJ,

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



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.