Q:

ASP.NET MVC Razor: How to render a Razor Partial View's HTML inside the controller action

How to generate a HTML of a given partial view on ASP.NET view engine is known.
But if this functionality is used on razor partial view it does not work, as exception says the partial view does not derive from "UserControl".
How to fix the rendering to support razor partial view?
I need this because I generate emails form this partial views ...
UPDATE:
Code that fails (@mcl):
public string RenderPartialToString(string controlName, object viewData)
    {
        ViewPage viewPage = new ViewPage() { ViewContext = new ViewContext() };
        viewPage.Url = this.GetUrlHelper();

        string fullControlName = "~/Views/Email/" + controlName + ".ascx";

        viewPage.ViewData = new ViewDataDictionary(viewData);
        viewPage.Controls.Add(viewPage.LoadControl(fullControlName));

        StringBuilder sb = new StringBuilder();
        using (StringWriter sw = new StringWriter(sb))
        {
            using (HtmlTextWriter tw = new HtmlTextWriter(sw))
            {
                viewPage.RenderControl(tw);
            }
        }
        return sb.ToString();
    }

A:

@Html.Partial("nameOfPartial", Model)

Update
protected string RenderPartialViewToString(string viewName, object model)
{
    if (string.IsNullOrEmpty(viewName))
        viewName = ControllerContext.RouteData.GetRequiredString("action");

    ViewData.Model = model;

    using (StringWriter sw = new StringWriter()) {
        ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
        viewResult.View.Render(viewContext, sw);

        return sw.GetStringBuilder().ToString();
    }
}

A:

Although adequate answers have already been given, I'd like to propose a less verbose solution, that can be used without the helper methods available in an MVC controller class.  Using a third party library called "RazorEngine" you can use .Net file IO to get the contents of the razor file and call
string html = Razor.Parse(razorViewContentString, modelObject);

Get the third party library here.

A:

You could also use the RenderView Controller extension from here
(source)
and use it like this:
public ActionResult Do() {
var html = this.RenderView("index", theModel);
...
}

it works for razor and web-forms viewengines

