什么是asp.net MVC中的@RenderSection

时间:2014-04-27 18:55:23

标签: asp.net-mvc

目的是什么以及它如何运作。我理解捆绑包的作用,但我还没弄清楚这是做什么的,它可能很重要。

@RenderSection("scripts", required: false)

或许是一个如何使用它的小例子?

5 个答案:

答案 0 :(得分:230)

如果你有像这样的_Layout.cshtml视图

<html>
    <body>
        @RenderBody()
        @RenderSection("scripts", required: false)
    </body>
</html>

然后你就可以拥有像这样的index.cshtml内容视图

@section scripts {
     <script type="text/javascript">alert('hello');</script>
}

required 表示使用布局页面的视图是否必须包含脚本部分

答案 1 :(得分:17)

如果

(1)你有一个像这样的_Layout.cshtml视图

<html>
    <body>
        @RenderBody()

    </body>
    <script type="text/javascript" src="~/lib/layout.js"></script>
    @RenderSection("scripts", required: false)
</html>

(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)你有About.cshtml

<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <h2>    Contacts</h2>
    </div>
</div>

在布局页面上,如果需要设置为false“@RenderSection(”scripts“,required:false)”,当页面渲染和用户在页面上时,contacts.js不会渲染。

    <html>
        <body><div>About<div>             
        </body>
        <script type="text/javascript" src="~/lib/layout.js"></script>
    </html>

如果需要设置为true“@RenderSection(”scripts“,required:true)”,当页面渲染和用户在ABOUT页面上时,contacts.js STILL将被渲染。

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

简而言之,当设置为 true 时,无论您是否在其他页面上需要它,它都会以任何方式呈现。如果设置为 false ,则只有在呈现子页面时才会呈现。

答案 2 :(得分:2)

这里是MSDN

中Rendersection的定义
  

在布局页面中,呈现命名部分的内容。MSDN

在_layout.cs页面中输入

@RenderSection("Bottom",false)

这里渲染bootom部分的内容并指定false boolean属性以指定是否需要该部分。

@section Bottom{
       This message form bottom.
}

这意味着如果你想在所有页面的底部,那么你必须使用false作为Rendersection方法的第二个参数。

答案 3 :(得分:1)

假设我有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>
    }

另一个没有脚本的视图“ 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>

还有我的布局页面“ _layout.cshtml”

@RenderSection("Scripts", required: true)

因此,当我导航到GetEmployeeDetails.cshtml时。我收到一个错误,即GetEmployeeDetails.cshtml中没有要呈现的部分脚本。 如果我将@RenderSection()中的标志从required : true更改为``required:false`。这意味着渲染视图@section脚本中定义的脚本(如果存在),否则什么也不做。 精致的方法将在_layout.cshtml

@if (IsSectionDefined("Scripts"))
    {
        @RenderSection("Scripts", required: true)
    }

答案 4 :(得分:0)

_Layout.cshtml

<html>
    <body>
        @RenderBody()
        @RenderSection("scripts", required: false)
    </body>
</html>

现在,我可以(如果required:true 必须)将名为 scripts 的部分添加到任何视图中。

例如About.cshtml

<h1>About</h1>
@section scripts {
     <script> alert('Welcome to about page!'); </script>
}

或者Contact.cshtml

<h1>Contact</h1>
@section scripts {
     <script> alert('Welcome to contact page!'); </script>
}

但不在 Story.cshtml 中:

<h1>Story</h1>    

最后,上面的视图呈现如下:

关于.cshtml

<html>
    <body>
      <h1>About</h1>    
      <script> alert('Welcome to about page!'); </script>    
    </body>
</html>

Contact.cshtml

<html>
    <body>
      <h1>Contact</h1>    
      <script> alert('Welcome to contact page!'); </script>    
    </body>
</html>

Story.cshtml

<html>
    <body>
      <h1>Story</h1>           
    </body>
</html>

如果我们设置了 required: true,我们必须将名为 scripts 的部分添加到 Story 视图和所有其他视图,否则将引发异常。此外,将 @section scripts 放在哪里并不重要,因为它最终会被 _Layout.cshtml 中的 @RenderSection... 替换。

旁注:请不要通过 JavaScript 警告框欢迎用户。 ?