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

I'm new to ASP MVC and utilizing the Intro to ASP MVC 4 Beta tutorial http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

I'm encountering an error that I can't seem to find an answer to nor do I have much programming experience so I don't know where to even start to fix this an move on with the tutorial. I appreciate any help you can provide.

I'm in the Accessing Your Model's Data from a Controller section and I am getting this error when I attempt to creat a Movie as a part of the tutorial, I click on the the link "Create New" and I get the following error

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

Rather than use Visual Studio express, I opted to download Visual Studio 2012 RC (not sure if that would be the root cause of my issue.

I realize you may require me to include code to answer this but I'm not sure what code to even include. Please advise what code you need me to include if any and I will be happy to add it to my question.

Thank you,

152698 次浏览

这意味着您已经在您的主 Layout.cshtml 中定义了一个节,但是在您的 View 中没有为该节包含任何内容。

如果您的 _ Layout.cshtml 具有如下内容:

@RenderSection("scripts")

然后,所有使用该布局 must的视图都包含一个具有相同名称的 @section(即使该部分的内容为空) :

@{
ViewBag.Title = "Title";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts{
// Add something here
}

作为替代方案, 你可以设置为 false,那么你就不需要在每个视图中添加这个部分,

@RenderSection("scripts", required: false)

或者也可以将 @RenderSection包装在 if块中,

@if (IsSectionDefined("scripts"))
{
RenderSection("scripts");
}

我有一个病例,有3个级别 a‘ la _ Cshtml < ——-Cshtml < ——-Cshtml:

_ MainLayout.cshtml

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

_ Middle.cshtml

@section head {
@RenderSection("head")
}

and in Cshtml defining

@section head {
***content***
}

我还是会得到错误

已定义了下列各节,但尚未呈现 for the layout page “~/Views/Shared/_Middle.cshtml”: "head".

结果,错误是 Cshtml依赖于 < strong >/Views/_ ViewStart.cshtml 这个问题是通过在 Cshtml中明确定义这个来解决的:

@{
Layout = "~/Views/_Shared/_MainLayout.cshtml";
}

无法确定这是由设计还是 MVC4中的 bug 造成的——不管怎样,问题已经解决了:)

此外,您还可以在 _Layout.cshtml_Layout.Mobile.cshtml中添加以下代码行:

@RenderSection("scripts", required: false)

如果建议的解决方案没有解决问题,我不知道为什么接受了这个被接受的答案。实际上可能有两个相关的问题与这个主题有关。

第一期

母版页(例如 _Layout.cshtml)定义了一个节,它是 需要,但是继承视图没有实现它。比如说,

布局模板

<body>
@* Visible only to admin users *@
<div id="option_box">
@* this section is required due to the absence of the second parameter *@
@RenderSection("OptionBox")
</div>
</body>

继承视图

不需要显示任何代码,只需考虑视图上有 @section OptionBox {}没有实现。

问题 # 1的错误

Section not defined: "OptionBox ".

第二期

The master page (e.g. _Layout.cshtml) has a section defined and it is 要求与 the inheriting view 是的 implement it. However, the implementing view have additional script sections that 没有定义 on (any of) its master page(s).

布局模板

same as above

继承视图

<div>
<p>Looks like the Lakers can still make it to the playoffs</p>
</div>
@section OptionBox {
<a href"">Go and reserve playoff tickets now</a>
}
@section StatsBox {
<ul>
<li>1. San Antonio</li>
<li>8. L. A. Lakers</li>
</ul>
}

问题 # 2的错误

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

OP 的问题类似于问题 # 2,公认的答案是问题 # 1。

在使用 Visual Studio 2012编写 ASP.NET MVC 4教程时,我在“从控制器访问模型的数据”部分遇到了同样的错误。解决办法很简单。

在 Visual Studio 2012中,在共享文件夹的 _ Layout.cshtml 文档中创建新的 ASP.NET MVC 4 Web Application 时,“脚本”部分被注释掉。

    @*@RenderSection("scripts", required: false)*@

只需取消注释该行,示例代码就可以工作了。

    @RenderSection("scripts", required: false)

它基本上发生在 _ Layout.cshtml没有:

@RenderSection("scripts", required: false)

或者

@RenderSection("scripts")

没有

required: false

所以,只要加上 @ RenderSection (“脚本”,必需: false) 在 _ Layout.cshtml 它特别适用于那些使用 肯杜伊生成的项目的开发人员。

我认为我们的解决方案与其他方案有很大的不同,所以我将在这里记录它。

We have setup of Main layout, an intermediary layout and then the final action page render. Main.cshtml <- Config.cshtml <- Action.cshtml

只有当 web.config 有 customErrors='On/RemoteOnly'时,我们得到一个自定义错误,没有异常,也没有调用 Application_Error。我可以在 Error.cshtml 中的 Layout = null行上捕捉到这一点。例外是在问题,缺少脚本部分。

我们在 Main.cshtml 中定义了它(使用必需的: false) ,Action.cshtml 没有写入脚本部分的内容。

解决方案是将 @section scripts { @RenderSection("scripts", false) }添加到 Config.cshtml。

我在实现 MVC 初学者教程时遇到了同样的问题。我得到了各种建议来修改 layout.cshtml 文件中的@RenderSection,但我还没有使用它。

我搜索了很多,然后发现在(View/Edit.cshtml)和其他 cshtml 文件中生成的 script 标记没有呈现

**@section Scripts {
@Scripts.Render("~/bundles/jqueryval")

}**

因此,我删除了这些代码行,应用程序开始平稳运行。

It appears that there is a mismatch between the View files that some versions of Visual Studio auto-generates for you when you use it to create a new Model. I encountered this problem using the 新版 VS 2013社区版 and walking through the W3Schools tutorial at http://www.w3schools.com/aspnet/mvc_app.asp but the comments above indicate that its not a problem with the tutorial directions or with a single version of VS.

的确,您可以通过删除

@Scripts.Render("~/bundles/jqueryval")

从 VisualStudio 自动生成的创建/编辑布局的。

但是这个解决方案并没有解决问题的根本原因,也没有让您在完成本教程之外做更多的事情。在实际应用程序开发的某个阶段(可能相当早) ,您将需要访问注释释出解决方案从应用程序中删除的 jquery 验证代码。

如果您使用 VS 为您创建一个新模型,它还会创建一组五个视图文件: 创建、删除、详细信息、编辑和索引。其中两个视图 Create 和 Edit 的目的是让用户为构成模型基础的数据库记录中的字段添加/编辑数据。对于实际应用程序中的这些视图,在将记录保存到 db 中之前,可能需要使用 jquery 验证库进行一些数据验证。这就是 VS 添加以下代码行的原因

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

这两种观点的底部,而不是其他的。自动生成的代码试图使验证库可用于这些视图,而不是其他视图。

发生这个错误是因为 VS 没有向 share _ Layout.cshtml 文件添加对应的行,或者,看到上面的一个答案,添加了该行,但是将其注释掉了。这条线是

@RenderSection("scripts", required: false)

如果某些视图有脚本部分(如 Create 和 Edit) ,则布局中必须嵌入 RenderSection 命令。如果有些脚本具有该节,而有些脚本没有(如 Delete、 Details 和 Index 没有) ,则 RenderSection 命令必须具有 required: false参数。

因此,最好的解决方案是,如果您希望完成本教程以外的操作,那么将语句添加到 _ Layout.cshtml,而不是从 Edit and Create 视图中删除代码。

附言。这里有一点让人困惑,需要的是一个“ bundle”,而 request 语句看起来像是试图将一个文件包含在一个 bundle 文件夹中,而这个 bundle 文件夹在您的项目中并不存在。但是,对于调试构建和教程来说,这是不相关的,因为绑定的文件一次只包含一个。见: http://www.asp.net/mvc/overview/performance/bundling-and-minification这里有争议的代码在这页的三分之二处被简要地提到。

我在网上搜索错误,找到了这个页面。 我使用的是 VisualStudio2015,这是我的第一个 MVC 项目。

如果在呈现部分之前错过了@符号,也会得到相同的错误。我想把这个分享给未来的初学者。

 @RenderSection("headscripts", required: false)

检查“”的拼写和大小写

无论何时我们写作 @ RenderSection (“名称”,要求: false) 确保剃须刀视图包含一个部分 @section name{} 所以 检查“”的拼写和大小写 Correct in this case is "Scripts"

我有一种感觉,你是从 _ Layout 文件中的@部分呈现你的部分,它引用了一个带有@部分的部分视图,也就是说,你在@部分中嵌套了一个@部分。在 _ Layout 文件中,删除 rendersection 周围的@部分。

请确保您在视图中输入了正确的拼写,即使用脚本部分

正确答案是

@section scripts{ //your script here}

如果你输入 @section script{ //your script here}这是错误的。

我用以下方法解决了这个问题,

@await Html.PartialAsync("_ValidationScriptsPartial")

对我来说,问题出在我的 _ Layout.cshtml 中,我在一个条件中有 RenderSection

 @if (theme == "Red" || theme == "Green")
{
<div>
@RenderSection("Footer", false)
</div>
}

在我看来,这是无条件的

@section Footer{
<div>
@Html.AwButton("Reject", "Reject")
@Html.AwSubmit("Ok", "Ok")
</div>
}

所以不管主题是什么,孩子都要加上页脚。但是在父主题不是红色或绿色时,它没有添加页脚并且抛出非手动异常。

我还注释了 * *@部分脚本 比它运行平稳要好。 :)

我已经像下面一样修改了“ _ Layout.cshtml”,它可以工作了。 . Net Core 3.1 Blazor Server 模块身份问题。

<!DOCTYPE html>
<html>


<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>@ViewBag.Title</title>
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="~/css/app.css" rel="stylesheet" />
@RenderSection("Scripts", required: false)




</head>


<body>
<div class="main">
<div class="content px-4">




@RenderBody()
</div>
</div>
</body>


</html>




@section Scripts {
    

}

我刚刚得到了这个错误,我读到背后的原因是你必须渲染一个脚本部分。但是在布局中它已经是原样了。

@RenderSection("Scripts", required: false)

在我添加页面底部的部分之前,它也起作用了。

在我的情况下的原因是一个简单的拼写 Scripts的打字错误。

@section Scripts{
<script src="~/js/myScript.js"></script>
}

对我来说,我说的是 < strong > script 而不是 < em > < strong > 脚本 我不知道为什么它不显示我作为一个错误,而调试它。

@section Script {}