部分已定义但未针对布局页面“〜/ Views / Shared / _Layout.cshtml”呈现:“head”

时间:2013-05-08 08:36:49

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

我创建了一个新的MVC4互联网应用程序。我只是关注ASP.NET MVC4 in Action一书。本章是Ajax是MVC中的ASP.NET。 索引的视图就像这样

@section head{
    <script type ="text/javascript"

            src="@Url.Content("~/Scripts/AjaxDemo.js")"></script>
}

@Html.ActionLink("Show the privacy policy", "PrivacyPolicy", null, new{id="privacyLink"})

<div id="privacy"></div>

和部分视图包含一些非常基本的标记。

<h2>Our Commitment to privacy</h2>
This is sample priavcy policy.

在AjaxDemo.js文件中,其路径在index.cshtml文件中给出,我的代码很小

$(document).ready(function() {
    $('privacyLink').click(function(event) {
        event.preventDefault();

        var url = $(this).attr('href');
        $('#privacy').load(url);
    });
});

现在,当我运行此应用程序并手动将链接指定为http://localhost:19208/customajax

我收到错误说

The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "head".

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "head".

任何想法我在这里做的错误是什么?

_Layout.cshtml是默认值,我没有改变任何内容

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")

    </head>
    <body>
        <header>
            <div class="content-wrapper">
                <div class="float-left">
                    <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
                </div>
                <div class="float-right">
                    <section id="login">
                        @Html.Partial("_LoginPartial")
                    </section>
                    <nav>
                        <ul id="menu">
                            <li>@Html.ActionLink("Home", "Index", "Home")</li>
                            <li>@Html.ActionLink("About", "About", "Home")</li>
                            <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                        </ul>
                    </nav>
                </div>
            </div>
        </header>
        <div id="body">
            @RenderSection("featured", required: false)
            <section class="content-wrapper main-content clear-fix">
                @RenderBody()
            </section>
        </div>
        <footer>
            <div class="content-wrapper">
                <div class="float-left">
                    <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
                </div>
            </div>
        </footer>

        @Scripts.Render("~/bundles/jquery")
        @RenderSection("scripts", required: false)
    </body>
</html>

1 个答案:

答案 0 :(得分:18)

您尚未在head

中定义_Layout.cshtml部分

您只需在_Layout.cshtml

的标题部分中对其进行定义即可

作为

<head>
    @RenderSection("head", required: false)
</head>