Skip to content

Instantly share code, notes, and snippets.

@joeygreen
Created March 29, 2016 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeygreen/c0f2f64e86f923821b33 to your computer and use it in GitHub Desktop.
Save joeygreen/c0f2f64e86f923821b33 to your computer and use it in GitHub Desktop.
public static string RenderViewToString(ControllerContext context, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
ViewDataDictionary viewData = new ViewDataDictionary(model);
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
ViewContext viewContext = new ViewContext(context, viewResult.View, viewData, new TempDataDictionary(), sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment