【问题标题】:What is @RenderSection in asp.net MVCasp.net MVC 中的@RenderSection 是什么
【发布时间】:2014-04-27 18:55:23
【问题描述】:

@RenderSection 的用途是什么,它是如何发挥作用的?我了解捆绑包的作用,但我还没有弄清楚它的作用,它可能很重要。

@RenderSection("scripts", required: false)

也许是一个关于如何使用它的小例子?

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

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

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

    那么你就可以有一个这样的 index.cshtml 内容视图

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

    必需表示使用布局页面的视图是否必须有脚本部分

    【讨论】:

    • 这没有回答“目的是什么?”的问题。和“它是如何运作的?”。
    • @Mathhew 同意它不回答这个问题。目的是允许网页以正确的顺序加载。假设您使用菜单模板等。加载页面加载 _layout.cshtml,然后在加载所有元素后 _layout.cshtml 使用 RenderBody 调用加载页面的 部分,以确保其元素位于正确的位置.加载完所有元素后,需要加载页面特定的脚本,然后 RenderSection 会加载脚本。简而言之,就是这样可以按正确的顺序加载东西
    【解决方案2】:

    如果

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

    在您的布局页面上,如果 required 设置为 false "@RenderSection("scripts", required: false)",当页面呈现并且用户在 about 页面上时,contacts.js 不会呈现。

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

    如果 required 设置为 true "@RenderSection("scripts", required: true)",当页面渲染并且用户在 ABOUT 页​​面时,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>
    

    简而言之,当设置为 true 时,无论您在其他页面上是否需要它,它都会被渲染。如果设置为false,则仅在渲染子页面时才会渲染。

    【讨论】:

    • 这是不正确的。你应该自己尝试你的答案,你会注意到在将所需的标志设置为true 时呈现关于页面时,你会得到一个Section not defined: "scripts".
    • 只是一个澄清。不应该是“脚本”而不是“脚本”吗?
    【解决方案3】:

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

      这里是来自MSDN的Rendersection的定义

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

      在_layout.cs页面放

      @RenderSection("Bottom",false)
      

      这里渲染bootom部分的内容,并指定false布尔属性来指定是否需要该部分。

      @section Bottom{
             This message form bottom.
      }
      

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

      【讨论】:

        猜你喜欢
        • 2010-09-10
        • 1970-01-01
        • 2019-06-01
        • 1970-01-01
        • 2012-06-01
        • 2011-04-13
        • 2011-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多