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:

<html>
<head>	
</head>
<body>
	
    <p>Hello World!</p>
	
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
	
    @RenderSection("ScriptsBottom", required: false;)
	
</body>
</html>

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="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></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