嵌套/继承布局页面无效

时间:2011-09-23 09:32:21

标签: .net asp.net asp.net-mvc asp.net-mvc-3

我有以下布局页面

_StyledLayout.cshtml:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />


    <title>@ViewBag.Title</title>

    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

    @RenderSection("Head", false)
</head>
<body>
    @RenderBody()
</body>
</html>

_StyledPageLayout.cshtml:

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

@section Head
{
<script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>

}

@RenderBody()

Details.cshtml:

@using System.Data;

@model ASPNETMVC3.Models.ConfigModel
@{
    ViewBag.Title = "Details";
    Layout = "~/Views/Shared/_StyledPageLayout.cshtml";
}

@section Head
{
<script type="text/javascript">

        var oTable;

        $(document).ready(function () {  

            $(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' });

        });

    </script>
}

<h2>
    Details</h2>

在此设置中,当我转到详细信息时,我收到错误: *以下部分已定义,但尚未针对布局页面“〜/ Views / Shared / _StyledPageLayout.cshtml”呈现:“Head” *

我做错了什么?

2 个答案:

答案 0 :(得分:2)

_StyledPageLayout.cshtml

@section Head
{
    @if (IsSectionDefined("Head"))
    {
        // If the view contains a Head section use that instead
        @RenderSection("Head")
    }
    else
    {
        // The view doesn't have a Head section => use some default output
        <script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
    }
}

答案 1 :(得分:2)

基本上与已接受的答案相同,但如果您不需要默认渲染,则会更整洁......

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

...其中'false'表示该部分不是必需的。