Created
March 29, 2016 12:44
-
-
Save joeygreen/c0f2f64e86f923821b33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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