title: ASP.NET MVC render Razor Partial View as String Error?
body: <p>I am using this code to try and render a razor partial view as a string for the purposes of sending an email.</p>
  
  <pre><code>    public static string RenderPartialToString(
          string userControlPath,
          object viewModel,
          ControllerContext controllerContext,
          TempDataDictionary tempData)
      {
          using (var writer = new StringWriter())
          {
              var viewDataDictionary = new ViewDataDictionary(viewModel);
              var view = new WebFormView(controllerContext, userControlPath);
              var viewContext = new ViewContext(
                  controllerContext,
                  view,
                  viewDataDictionary,
                  tempData,
                  writer
                  );
  
              viewContext.View.Render(viewContext, writer);
  
              return writer.GetStringBuilder().ToString();
          }
  
      }
  </code></pre>
  
  <p>The problem is that I get the follow error:</p>
  
  <pre><code>must derive from ViewPage, ViewPage&lt;TModel&gt;, ViewUserControl, or ViewUserControl&lt;TModel&gt;. Stack Trace:  at  System.Web.Mvc.WebFormView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at .... RenderPartialToString
  </code></pre>
  
  <p>How would I fix that ?</p>