Creating Placeholders in MVC using @RenderSection

Last updated on 14 Dec 2016 | Posted on 03 Dec 2016

It is common in web development and beneficial in other ways such as aiding performance to only load what needs to be loaded on each page of your website. This includes all things but most of the time in means JavaScript, CSS and images.

If you're familiar with ASP.NET forms you'll know that you can use ContentPlaceHolder's to allow certain content or scripts to run via this control.

The main aim of what you're trying to achieve is to create a convenient set of portals in which to inject content coming via either views. This is most commonly seen with View and Partial View relationship.

Take the following snippet as an example of a shared view where @RenderSection can be defined:

    <p>Hello World!</p>
    <script src=""></script>
    @RenderSection("ScriptsBottom", required: false;)

The @RenderSection block takes a string name as the first parameter and subsequent optionals. With the above code, this section can now be populated in whichever views inherit from the above shared view.

The following snippet shows the syntav for setting the section. @section [name] {} is the syntax used to set this optional content:

<script src=""></script>

@section ScriptsBottom {
    <!-- some code -->
Html.Action Vs Html.RenderAction

Html.Action Vs Html.RenderAction

Posted on 20 Nov 2016

What is the main difference between using @Html.Action and @Html.RenderAction? The answer is simple:

They both effectively perform the same thing in …

Read more