Rendering html for navigation using MVC 3 Razor

24 January 2011

Rendering html for navigation using MVC 3 Razor

I will be giving a class on developing websites with Composite C1 and MVC3 on Tuesday and I thought it would be interesting to use the Razor view engine (part of MVC 3) for the course samples. Since ASP.NET MVC is not something I haven’t spent much time with and the Razor syntax is brand new to me, I had some catching up to do.

One of the things that I have traditionally preferred to do with XSLT is navigational html – XSLT just lends itself really well to that kind of things due to the recursive nature of the html renderings. I wanted to see how Razor would handle navigation rendering logic, so I started to build a classic ‘navigation tree’ using Razor, learning a lot in the process. In the beginning I missed XSLT a lot – the mix of markup and logic was simply to horrible with Razor – but after it dawned on me that a View is only as elegant as the Model allow I rewrote my Model to provide the View easy access to relevant data - and things lightened up.

You can see different navigation related renderings below – the views include sitemap, breadcrumbs, sub navigation and top navigation. If you have installed the MvcPlayer you can copy the files below to your site and activate them using the path "/Navigation/Sitemap" etc.

The model

Presentation no longer available

The controller

Presentation no longer available

The views

Presentation no longer availablePresentation no longer availablePresentation no longer availablePresentation no longer available
  ASP.NET  MVC

comments powered by Disqus