I have a partial "Sidebar" added to the master page (Layout) and inside this partial I'm using:
@RenderSection("SearchList", required: false)
On one of the views that uses the master page I'm doing:
@section SearchList { // bunch of html }
but it's giving me the error:
The file "~/Views/Shared/_SideBar.cshtml" cannot be requested directly because it calls the "IsSectionDefined" method.
What's wrong here?
You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .
RenderBody() renders all the content of the child view which is not wrapped in the named section. RenderSection() renders only a part of the child view which is wrapped under the named section.
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 method exists in the Layout page to render child page/view. It is just like the ContentPlaceHolder in master page.
What you are trying to do is not currently supported in Razor. Sections only work between the view page and its immediate layout page.
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