渲染ASP.NET MVC星火以字符串,失败孙子意见 [英] Render ASP.NET MVC Spark view to string, fails grandchildren views

查看:286
本文介绍了渲染ASP.NET MVC星火以字符串,失败孙子意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC 2星火视图引擎。我有一个正确呈现时,ASP.NET本身使得它的景色,但是当我执行Ajax调用重新呈现为一个字符串的局部视图,这样我可以替换使用jQuery的HTML,它呈现的局部视图,其子,但不改变其孙子的意见。

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.

在code以下被用来渲染父局部视图转换为字符串:

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?

推荐答案

以上code实际上做的工作。错误是其他地方在JavaScript code,它使用的输出。

The above code actually does work. The error was elsewhere in the javascript code that used the output.

所以,如果你需要渲染视图为一个字符串,上面code应该工作。

So, if you need to render a view to a string, the above code should work.

这篇关于渲染ASP.NET MVC星火以字符串,失败孙子意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆