It seems, most code for rendering view into string doesn't work in MVC 5.
I have latest MVC 5.1.2 templates and I am trying to render view into string.
public static String RenderViewToString(ControllerContext context, String viewPath, object model = null)
{
context.Controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindView(context, viewPath, null);
var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(context, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
Well, it's working but its output contains lots of $ marks instead tags. I read something about it was fixed in RC version, but that's old news.
Problem looks like this
<$A$><h1></h1>
<table</$A$><$B$> class=""</$B$><$C$>> <tbody</$C$><$D$></$D$><$E$>></tbody>
</table></$E$>
I would like to ask, how do you render views into string in latest MVC 5 template ? Thanks.
See Question&Answers more detail:os