I am using ASP.NET MVC 2 with the Spark view engine. I have a view that renders correctly when ASP.NET itself renders it, but when I perform an ajax call to re开发者_Python百科-render a partial view to a string so that I can replace the html with jQuery, it renders the partial view, its children, but not its grandchildren views.
The view hierarchy is Index-> Parent Partial View -> Child Partial View -> Grandchild Partial View.
The code below is being used to render the Parent Partial View to a string:
protected string RenderPartialViewToString(string viewName, object model)
{
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();
}
}
When it renders, the Parent Partial View and all Child Partial Views render properly, but none of the Grandchild Partial Views render. What should be happening to correctly render all children and grandchildren with the partial view into a string?
The above code actually does work. The error was elsewhere in the javascript code that used the output.
So, if you need to render a view to a string, the above code should work.
精彩评论