Render view as string
public static string RenderViewAsString(this Controller controller, ControllerContext context, string viewName, object model)
{
var stringWriter = new StringWriter();
var viewResult = ViewEngines.Engines.FindView(context, viewName, null);
var viewContext = new ViewContext(
controller.ControllerContext,
viewResult.View,
new ViewDataDictionary(model),
controller.TempData,
stringWriter
);
viewResult.View.Render(viewContext, stringWriter);
return stringWriter.ToString();
}
When you use within an API controller, you have a problem with the ControllerContext :)
Here's a tip from Wouter de Kort (http://wouterdekort.blogspot.com.br/2012/10/rendering-aspnet-mvc-view-to-string-in.html?showComment=1363116520203#c637135064697708476(
Written by Riderman
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Asp.net-mvc
Authors
purekrome
7.098K
ridermansb
1.161K
Related Tags
#asp.net-mvc
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#