IMPROVED ASP.NET MVC SECTIONS

I'm taking a break from my umbrella series in an effort to contribute to society, instead of distracting it!

I really wanted to like ASP.NET MVC Sections... but I just couldn't. They just seem so rigid.

For instance, say I had a section right before the end of my body tag to stash all JavaScript files -- this is great, except I can only add code to this section one time, from one View. But what if my View renders other [partial] Views, all of which need to add JavaScript to the end of the body? Because of this drawback, sections became almost entirely useless to me.

Orchard [a popular CMS engine I've talked about before] has the Script.Head() and Script.Foot() methods, and these seemed like a big improvement. You could add code to these anywhere, and as many times as you liked. Unfortunately, while it is a step in the right direction, it still isn't quite dynamic enough. It only allows two sections: the head and the foot (and not to mention the application has to be built within Orchard). So yet again, even though these Orchard helpers are good, they aren't good enough.

The Solution

Piggybacking of off some other solutions, I was able to create something that gave me everything I wanted:

  1. Unlimited sections.
  2. No restriction on the number of codes blocks that could be added to a section.

The source code is hosted on GitHub, so I won't reproduce it here, but I will show you how easy it is to use.

In your layout View, or wherever you want the section to be rendered, include the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>@ViewBag.Title</title>
    @this.Section("header")
</head>
<body>
    @this.Section("prebody")

	@RenderBody()
		
    @this.Section("footer")
</body>
</html>

Then, to add content blocks to your sections:

@using(this.AddToSection("footer"))
{
    <script src="//js.stripe.com/v2/" type="text/javascript"></script>
}

@using(this.AddToSection("footer"))
{
    <div>add as many code blocks as you want!</div>
}

That's it and that's all! There are some improvements that could be made, like a parameter to order the content added to a section, but even in its basic form, I hope it's helpful!

Available as a NuGet package!

Fork On GitHub!

Improved ASP.NET MVC Sections
1.25.2014 ~ 01:50 PM
posted in professional
tagged in asp-net : mvc : orchard : orchard-cms : c#
comments [1]

 
 
 
 
@ Louis Pilkington
For a limited time we have lowered the rate on targeted website traffic. We have visitors from virtually every country on Earth. Each visitor is targeted by both country and keywords that you submit when you start your free trial period. If you need more visitors or product sales try our service free for seven days and we will send you 500 free visitors during the trial. There are no contracts and if you cancel during the trial period you will not be charged anything! Start your trial today: http://www.1lnk.net//1ol7 Unsubscribe here: https://twe.lv/gg
10.28.2018 - 10:31 AM
 
 

SPEAK!

Please enter your comment below.