title: Return Razor partial view using JSON (ASP MVC 3)
body: <p>In MVC 2 with the regular view engine i could return a ascx partial view as string through <code>return Json()</code></p>
  
  <p>But with the new Razor .cshtml views I can not figure out how to do this. I keep getting <code>Type 'ASP.CustomerForm_cshtml' does not inherit from 'System.Web.UI.UserControl'.</code></p>
  
  <p>The partial view inherits from <code>System.Web.Mvc.WebViewPage&lt;T&gt;</code> and another error pops up if I inherit from <code>System.Web.UI.UserControl&lt;T&gt;</code> as the first error says.</p>
  
  <p>Any thoughts on how to fix this using ASP MVC 3 and Razor view engine?</p>
  
  <p>This is my ControlToString function:</p>
  
  <pre><code>    private string ControlToString(string controlPath, object model)
      {
          ViewPage page = new ViewPage();
          ViewUserControl ctl = (ViewUserControl)page.LoadControl(controlPath);
          page.Controls.Add(ctl);
          page.ViewData.Model = model;
          page.ViewContext = new ViewContext();
          System.IO.StringWriter writer = new System.IO.StringWriter();
          System.Web.HttpContext.Current.Server.Execute(page, writer, false);
          string outputToReturn = writer.ToString();
          writer.Close();
          //return this.Json(outputToReturn.Trim());
          return outputToReturn.Trim();
      }
  </code></pre>