Essentially, it involved using a gem called Gon which enabled you to do this in your controller:
I thought that was pretty cool, and it'd be awesome to have the same thing in ASP.NET MVC. Enter NGon. The code is actually very simple, but I think the usage is quite elegant (It's up on GitHub here, complete with Unit Tests and a sample app).
The first step is to register the NGonActionFilterAttribute in the global filters in your Global.asax.cs page:
Then, in your Controller, you do this:
Next, in your HTML page (probably in your Layout.cshtml page so it's available everywhere) you add this line:
Under the hood:
It's actually all quite simple. The first thing that happpens, is when an action gets called, the NGonActionFilterAttribute fires, and it creates a new ExpandoObject called NGon on the Controllers ViewBag property:
Then, as you keep adding items to the ViewBag.NGon property, you're adding it to this underlying ExpandoObject. Finally, when you call the IncludeNGon in the HTML, this is where all the magic happens:
Cool huh? Drop me a comment, let me know what you think!