MVC 3 - 嵌套布局 - 部分不渲染领域 [英] MVC 3 - Nested layouts - sections don't render in Areas

查看:201
本文介绍了MVC 3 - 嵌套布局 - 部分不渲染领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于这种嵌套的布局结构:

Given this nested layout structure:


~/Views/Shared/_layoutBase.cshtml
~/Views/Shared/_layout.cshtml

其中, _layoutBase.cshtml 是布局 _layout.cshtml

在布局文件中定义的任意部分渲染下页的内容细〜/浏览次数/...

Any sections defined in the layout files render their content fine in pages under ~/Views/...

然而,对于次在一个区域,该部分被从未呈现。

However, for views in an area, the sections are never rendered.

_layoutBase

<script type="text/javascript">
        @RenderSection("footerScripts", false)
    </script>
</body>
</html>

_layout.cshtml

@section footerScripts{
    @RenderSection("footerScripts", false)
}

内容视图:

@section footerScripts{
$(function () {
    SetFocusOnForm("CaptchaCode", "NextButton");
});
}

部分的内容 footerScripts 永远不会在一个区域的视图渲染。它得到的呈现在一种观点认为是在〜/浏览次数文件夹中。

The content of section footerScripts never gets rendered in a view in an area. It does get rendered in a view that is under the ~/Views folder.

_ViewStart.cshtml

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
} 

问:

你能看到什么错?!

Question:

Can you see anything wrong?!

推荐答案

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

I am unable to reproduce the problem. Here's my setup and steps I did.


  1. 创建使用互联网的应用程序模板的新ASP.NET MVC 3应用程序

  2. 添加〜/查看/共享/ _LayoutBase.cshtml

<!DOCTYPE html>
<html>
<body>
@RenderBody()
<script type="text/javascript">
    @RenderSection("footerScripts", false)
</script>
</body>
</html>


  • 替换〜/查看/共享/ _Layout.cshtml 本的内容:

    @{
        Layout = "~/Views/Shared/_LayoutBase.cshtml";
    }
    
    @section footerScripts{
        @RenderSection("footerScripts", false)
    }
    
    @RenderBody()
    


  • 该项目
  • 右键单击并添加一个管理区

  • Right click on the project and add an Admin area

    一个添加的TestController这个管理区,并添加相应的〜/地区/行政/查看/测试/ Index.cshtml 查看:

    Add a TestController to this admin area and add a corresponding ~/Areas/Admin/Views/Test/Index.cshtml view:

    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Index</h2>
    
    @section footerScripts{
        alert('ok');
    }
    


  • 运行应用程序并导航到 /管理/测试/指数

  • 显示警报

  • 这篇关于MVC 3 - 嵌套布局 - 部分不渲染领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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