RenderSection() inside partial with master page

When creating your layout view you might want to put some pieces seperatly into partial views.

You might also encounter the need to render sections that need to be placed into the markup of one of these partial views. However, since partial views do not support RenderSection logic you'll have to work around this.

You can render sections in a partial views by passing the RenderSection result from your Layout page as the model of the partial view. You can do this by putting this line of code in your _Layout.cshtml.

_Layout.cshtml

@{ Html.RenderPartial("_YourPartial", RenderSection("ContextMenu", false));}

Then in _YourPartial.cshtml you can render the section passed along as the model in the Html.RenderPartial call on the _Layout view. You check if the model is null just in case your section is not required.

_YourPartial.cshtml

@model HelperResult
@if (Model != null)
{
    @Model
}

What you are trying to do is not currently supported in Razor. Sections only work between the view page and its immediate layout page.