Render ASP.NET MVC Spark view to string, fails grandchildren views

ε祈祈猫儿з 提交于 2019-12-12 05:33:40

问题


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-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?


回答1:


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.



来源:https://stackoverflow.com/questions/4936809/render-asp-net-mvc-spark-view-to-string-fails-grandchildren-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!