asp.net-mvc 什么是asp.net MVC中的@RenderSection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23327578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is @RenderSection in asp.net MVC
提问by Aflred
What is the purpose of @RenderSection and how does it function? I understand what bundles do, but I have yet to figure out what this does and it's probably important.
@RenderSection 的目的是什么,它是如何运作的?我了解捆绑包的作用,但我还没有弄清楚它的作用,这可能很重要。
@RenderSection("scripts", required: false)
Perhaps a small example on how to use it?
也许是一个关于如何使用它的小例子?
回答by cgijbels
If you have a _Layout.cshtml view like this
如果您有这样的 _Layout.cshtml 视图
<html>
<body>
@RenderBody()
@RenderSection("scripts", required: false)
</body>
</html>
then you can have an index.cshtml content view like this
然后你可以有一个 index.cshtml 内容视图
@section scripts {
<script type="text/javascript">alert('hello');</script>
}
the requiredindicates whether or not the view using the layout page must have a scripts section
的要求表示使用页面布局的视图是否必须有一个脚本部分
回答by Maria Jesusa Galapon
If
如果
(1) you have a _Layout.cshtml view like this
(1) 你有一个像这样的 _Layout.cshtml 视图
<html>
<body>
@RenderBody()
</body>
<script type="text/javascript" src="~/lib/layout.js"></script>
@RenderSection("scripts", required: false)
</html>
(2) you have Contacts.cshtml
(2) 你有 Contacts.cshtml
@section Scripts{
<script type="text/javascript" src="~/lib/contacts.js"></script>
}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2> Contacts</h2>
</div>
</div>
(3) you have About.cshtml
(3) 你有 About.cshtml
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2> Contacts</h2>
</div>
</div>
On you layout page, if required is set to false "@RenderSection("scripts", required: false)", When page renders and user is on about page, the contacts.js doesn't render.
在您的布局页面上,如果 required 设置为 false "@RenderSection("scripts", required: false)",当页面呈现并且用户在关于页面上时,contacts.js 不会呈现。
<html>
<body><div>About<div>
</body>
<script type="text/javascript" src="~/lib/layout.js"></script>
</html>
if required is set to true "@RenderSection("scripts", required: true)", When page renders and user is on ABOUT page, the contacts.js STILL gets rendered.
如果 required 设置为 true "@RenderSection("scripts", required: true)",当页面呈现并且用户在关于页面上时,contacts.js 仍然会呈现。
<html>
<body><div>About<div>
</body>
<script type="text/javascript" src="~/lib/layout.js"></script>
<script type="text/javascript" src="~/lib/contacts.js"></script>
</html>
IN SHORT, when set to true, whether you need it or not on other pages, it will get rendered anyhow. If set to false, it will render only when the child page is rendered.
简而言之,当设置为true 时,无论您是否需要在其他页面上使用它,它都会以任何方式呈现。如果设置为false,则仅在呈现子页面时呈现。
回答by Brijesh Mavani
Here the defination of Rendersection from MSDN
这里Rendersection的定义来自 MSDN
In layout pages, renders the content of a named section.MSDN
在布局页面中,呈现命名部分的内容。MSDN
In _layout.cs page put
在 _layout.cs 页面中放置
@RenderSection("Bottom",false)
Here render the content of bootom section and specifies falseboolean property to specify whether the section is required or not.
这里渲染 bootom 部分的内容并指定false布尔属性来指定是否需要该部分。
@section Bottom{
This message form bottom.
}
That meaning if you want to bottom section in all pages, then you must use false as the second parameter at Rendersection method.
这意味着如果你想在所有页面中底部部分,那么你必须在 Rendersection 方法中使用 false 作为第二个参数。
回答by Vijay
Suppose if I have GetAllEmployees.cshtml
假设我有 GetAllEmployees.cshtml
<h2>GetAllEmployees</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
// do something ...
</thead>
<tbody>
// do something ...
</tbody>
</table>
//Added my custom scripts in the scripts sections
@section Scripts
{
<script src="~/js/customScripts.js"></script>
}
And another view "GetEmployeeDetails.cshtml" with no scripts
另一个没有脚本的视图“GetEmployeeDetails.cshtml”
<h2>GetEmployeeByDetails</h2>
@Model.PageTitle
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
// do something ...
</thead>
<tbody>
// do something ...
</tbody>
</table>
And my layout page "_layout.cshtml"
还有我的布局页面“_layout.cshtml”
@RenderSection("Scripts", required: true)
So, when I navigate to GetEmployeeDetails.cshtml. I get the error that there is no section scripts to be rendered in GetEmployeeDetails.cshtml.
If I change the flag in @RenderSection()from required : trueto ``required : false`. It means render the scripts defined in the @section scripts of the views if present.Else, do nothing.
And the refined approach would be in _layout.cshtml
所以,当我导航到 GetEmployeeDetails.cshtml 时。我收到错误消息,在 GetEmployeeDetails.cshtml 中没有要呈现的部分脚本。如果我将标志@RenderSection()从required : true“required : false”更改为“required : false”。这意味着渲染在视图的@section 脚本中定义的脚本(如果存在)。否则,什么都不做。改进的方法将在 _layout.cshtml
@if (IsSectionDefined("Scripts"))
{
@RenderSection("Scripts", required: true)
}

