asp.net MVC의 @RenderSection는 무엇입니까


170

@RenderSection의 목적은 무엇이며 어떻게 작동합니까? 나는 번들이 무엇을하는지 이해하지만 이것이 무엇을하는지 아직 파악하지 못했으며 아마도 중요 할 것입니다.

@RenderSection("scripts", required: false)

아마도 그것을 사용하는 방법에 대한 작은 예일까요?

답변:


287

이와 같은 _Layout.cshtml보기가있는 경우

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

그런 다음 index.cshtml 컨텐츠 뷰를 가질 수 있습니다

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

필요는 레이아웃 페이지를 사용하여 뷰가 스크립트 섹션이 있어야합니다 여부를 나타냅니다


20

만약

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

required가 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로 설정 하면 하위 페이지가 렌더링 될 때만 렌더링됩니다.


16
이것은 정확하지 않습니다. 답변을 직접 시도해야하며에 Section not defined: "scripts".필요한 플래그를 설정할 때 정보 페이지를 렌더링 할 때 시간이 표시됩니다 true.
cgijbels

설명 만하면됩니다. "스크립트"대신 "스크립트"가 아니어야합니까?
SRIDHARAN

2

렌더 섹션의 정의는 MSDN

레이아웃 페이지에서 명명 된 섹션의 내용을 렌더링합니다. MSDN

_layout.cs 페이지에서

@RenderSection("Bottom",false)

여기에서 bootom 섹션의 내용을 렌더링하고 false부울 속성을 지정하여 섹션이 필요한지 여부를 지정합니다.

@section Bottom{
       This message form bottom.
}

즉, 모든 페이지에서 섹션을 맨 아래로 나타내려면 Rendersection 메서드에서 두 번째 매개 변수로 false를 사용해야합니다.


2

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에``필요 : FALSE '를. 이는 뷰의 @ 섹션 스크립트에 정의 된 스크립트를 렌더링하는 것을 의미합니다. 세련된 접근 방식은 _layout.cshtml에 있습니다.

@if (IsSectionDefined("Scripts"))
    {
        @RenderSection("Scripts", required: true)
    }
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.