I have an ASP.NET-MVC3-Layout and use the RenderSection function:
@RenderSection("BackLink", required: false)
How can I call this function twice? I want to render a defined section at multiple places in my layout.
If I use @RenderSection()
more than once I get an error.
There can only be one RenderBody method per Layout page.
Multiple RenderBody() methods are NOT allowed in a single layout view. Multiple RenderSection() methods are allowed in a single layout view. The RenderBody() method does not include any parameter. The RenderSection() method includes boolean parameter "required", which makes the section optional or mandatory.
RenderBody() is called to render the content of a child view. Any content on said view that is not in a @section declaration will be rendered by RenderBody() . Using the Layout view above, that means that all content in a child view will be rendered inside the <div class="container body-content"> .
RenderBody is required, as it's what renders each view. RenderSection has an optional parameter that lets you mark the section as not required.
Maybe something like:
@var result = RenderSection("BackLink", required: false).ToHtmlString();
First:
@Html.Raw(result);
Second:
@Html.Raw(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With