InterviewSolution
| 1. |
Explain RenderSection() in detail. |
|
Answer» The RenderSection() method belongs to the WebPageBase class and is used for rendering the content of the defined section. The RenderSection() method’s FIRST parameter will be the section name we wish to render at that particular location in the layout template. The second parameter will enable us for defining whether the rendering section is needed or not and this parameter is OPTIONAL. If it is a “required” section, then an error will be thrown by the Razor if that section has not been implemented WITHIN a view template. It returns the HTML content for rendering purposes. For EXAMPLE, a section named “note” in a layout page can be defined as follows: @section note{<h1>Header Content</h1>}The above-defined section can be rendered on the content page by using the below code: @RenderSection("note") All sections are compulsory by default. To make the sections optional, you need to provide the value of the second parameter as a boolean value false. <div id="body"> @RenderSection("note", false) //used for injecting the content in the defined section <section class="content-wrapper main-content clear-fix"> @RenderBody() </section></div>Using the above code, the section named “note” will be rendered on the content page. Here, the RenderBody method can be used for rendering the child page/view. |
|