Asp.net mvc 3 MVC 3-嵌套布局-截面不';不可在区域内渲染 问题:

Asp.net mvc 3 MVC 3-嵌套布局-截面不';不可在区域内渲染 问题:,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,给定此嵌套布局结构: ~/Views/Shared/_layoutBase.cshtml ~/Views/Shared/_layout.cshtml \u layout.cshtml: @section footerScripts{ @RenderSection("footerScripts", false) } @{ Layout = "~/Views/Shared/_Layout.cshtml"; } <!DOCTYPE h

给定此嵌套布局结构:

~/Views/Shared/_layoutBase.cshtml ~/Views/Shared/_layout.cshtml
\u layout.cshtml

@section footerScripts{
    @RenderSection("footerScripts", false)
}
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
} 
<!DOCTYPE html>
<html>
<body>
@RenderBody()
<script type="text/javascript">
    @RenderSection("footerScripts", false)
</script>
</body>
</html>
“内容”视图:

footerScripts
的内容永远不会在区域中的视图中呈现。它确实会在
~/Views
文件夹下的视图中渲染

区域
\u ViewStart.cshtml

@section footerScripts{
    @RenderSection("footerScripts", false)
}
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
} 
<!DOCTYPE html>
<html>
<body>
@RenderBody()
<script type="text/javascript">
    @RenderSection("footerScripts", false)
</script>
</body>
</html>
问题:
你能看出什么不对劲吗

我无法重现这个问题。这是我的设置和步骤

  • 使用Internet应用程序模板创建新的ASP.NET MVC 3应用程序
  • 添加
    ~/Views/Shared/\u LayoutBase.cshtml

    @section footerScripts{
        @RenderSection("footerScripts", false)
    }
    
    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    } 
    
    <!DOCTYPE html>
    <html>
    <body>
    @RenderBody()
    <script type="text/javascript">
        @RenderSection("footerScripts", false)
    </script>
    </body>
    </html>
    
  • 右键单击项目并添加管理区域

  • 将TestController添加到此管理区域并添加相应的
    ~/Areas/admin/Views/Test/Index.cshtml
    视图:

    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Index</h2>
    
    @section footerScripts{
        alert('ok');
    }
    
    @{
    ViewBag.Title=“Index”;
    Layout=“~/Views/Shared/_Layout.cshtml”;
    }
    指数
    @节页脚脚本{
    警报(“正常”);
    }
    
  • 运行应用程序并导航到
    /admin/test/index
  • 将显示警报
  • 原因是: 我今天早上起床后立刻发现了问题:

    我在局部视图中看到了@section块。在MVC3中,这是行不通的

    我真的很感激Darin的努力,这有效地证明了部门在预期的领域中工作。但真正的原因是这个

    我忘了它们在局部视图中,因为我有一个mvc 3向导,它在步骤中使用局部视图。如果javascript可用,它可以使用ajax,工作得非常好且一致,以至于您忘记了自己在做什么


    请给达林投票,但这才是真正的答案。

    +1:我相信你。我明天也要把它全部做完。我的代码中肯定有错误,但如果我能看到它,我就大发雷霆。非常感谢。这正是我想要的。布局中的嵌套渲染。。。谢谢!除此之外,如果您想添加一个区域,例如“admin”,您可以将其添加到区域的{Layout=“~/Views/Shared/_AdminLayout.cshtml”;}的_ViewStart.cshtml中,从根共享文件夹添加一个类似于此的_AdminLayout.cshtml@{Layout=“~/Views/Shared/_LayoutBase.cshtml”}@section-footerscript{@RenderSection(“footerscript”,false)}@RenderBody()如果这解决了您的问题,您应该接受自己的答案。