asp.net-mvc - 已为布局页面定义部分但未呈现 "~/Views/Shared/_Layout.cshtml": "head"

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

我创建了一个新的 MVC4 互联网应用程序。我正在关注《ASP.NET MVC4 in Action》这本书。本章是Ajax 是MVC 中的ASP.NET。 索引的 View 如下所示

@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>

部分 View 包含一些非常基本的标记。

<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>

最佳答案

您尚未在 _Layout.cshtml 中定义 head 部分

你只需要在_Layout.cshtml的head部分定义它

作为

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

关于asp.net-mvc - 已为布局页面定义部分但未呈现 "~/Views/Shared/_Layout.cshtml": "head",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16436101/

相关文章:

asp.net-mvc - 如何外包 ASP.NET MVC 应用程序的设计?

asp.net-mvc - 除了将 MetadataTypeAttribute 直接放在类上之外,是否有其他方法可以指导 ASP.NET MVC 有关某些类元数据的信息?

c# - 在 ASP.Net MVC View 中显示是/否而不是复选框

asp.net-mvc - Azure 存储 api 不适用于 asnyc 上传

c# - BLL(CRUD 和业务对象)通常是如何构建的?

c# - ASP.NET Identity WebAPI 密码重置 token 无效

asp.net - ASP MVC - 反转 foreach

c# - 具有重载的 ASP.NET MVC Web API 属性路由

c# - 在 ASP.NET MVC 中的 UnitOfWork 中转换 DbContext

c# - 从开始作业日期开始在 mvc 表中显示天数