I have this section defined in my _Layout.cshtml
@RenderSection(\"Scripts\", false)
I can easily use it from a view:
There is a way to insert sections in partial views, though it's not pretty. You need to have access to two variables from the parent View. Since part of your partial view's very purpose is to create that section, it makes sense to require these variables.
Here's what it looks like to insert a section in the partial view:
@model KeyValuePair
@{
Model.Key.DefineSection("SectionNameGoesHere", () =>
{
Model.Value.ViewContext.Writer.Write("Test");
});
}
And in the page inserting the partial view...
@Html.Partial(new KeyValuePair(this, Html))
You can also use this technique to define the contents of a section programmatically in any class.
Enjoy!