Sitecore MVC Tutorial – Controller Renderings

In my previous Sitecore MVC post I showed how to create the menu for my sample website using a View Rendering. Now I’m going to turn that into a Controller Rendering in order to highlight the menu item for the current page.

The first thing I’m going to do is extend the Menu_Item class that  has been auto-generated for Glass Mapper by TDS. As these classes are generated as partials I just need to create another partial class to extend it. In the Models folder of my project I have created a MenuItem file containing the partial class. In my new file I need to make sure that the namespace matches that of the auto-generated class so that the two partial classes become one. All I am going to do is add a new property called IsActive which later I will set to true for the menu item of the current page.

namespace MySitecoreWebsite.Web.Models.sitecore.templates.Custom.Global
    public partial class Menu_Item
        public bool IsActive { get; set; }

Next I need a controller. In the Controllers folder of the project I have created a new controller called MenuController containing the following.

public class MenuController : GlassController
    public ActionResult MainMenu()
        var currentItem = SitecoreContext.GetCurrentItem<IGlassBase>();
        var menuItems = SitecoreContext.Query<Menu_Item>("/sitecore/content/Menu/*[@@templatename='Menu Item']").ToList();

        var activeItem = menuItems.AsQueryable().FirstOrDefault(m => m.Link.TargetId == currentItem.Id);
        if (activeItem != null)
            activeItem.IsActive = true;

        var model = new MainMenu() { MenuItems = menuItems };

        return View(model);

First notice I’m inheriting from GlassController. This gives me some handy properties such as the SitecoreContext property for the current context and the ability to retrieve rendering parameters.

In the code above I first get the current item. When auto generating classes for Glass Mapper with TDS an IGlassBase interface and GlassBase abstract class are created with some standard properties such as ID, Language and Version. As all Sitecore items contain these fields I can get any item from Sitecore using IGlassBase provided I don’t require any other fields. Here I just want the ID of the item so it’s fine.

Next I get all of my menu items from Sitecore using a query; this is the same query I used for the View Rendering using the SitecoreQuery attribute.

Now that I have my menu items and my current item I just need to check for any menu items where the ID of the target page matches the current page. If I find one, I set the IsActive property to true.

Lastly I construct my MainMenu model class (this is the same class I had for the View Rendering but I’ve removed the SitecoreQuery attribute) and pass it to the view.

Now that my model and controller are ready I need to update my view to use the new IsActive property.

    @foreach (var menuItem in @Model.MenuItems)
        <li class="@(menuItem.IsActive ? "selected" : string.Empty)">
            @using (BeginRenderLink(menuItem, x => x.Link))

The free template I’m using has a css class called ‘selected’ to make an item active. I have changed my view to set this class if the IsActive property is true.

Now I’m going to add the Controller Rendering in Sitecore. In my Renderings > Custom folder I have deleted the View Rendering I created in my previous post. In its place I have created a new Controller Rendering with the same name.

controller-renderingFor the Controller field I have put the fully qualified type name of the controller and for the Controller Action the name of the Action to use.

The last thing to do is update my _Layout.ascx page and change the ID I’m using with the Rendering method to match the ID of my new Controller Rendering.


Now when I load my sample website the menu item gets highlighted based on the current page.


Posted on by Joe in ASP.NET, C#, MVC, Sitecore

One Response to Sitecore MVC Tutorial – Controller Renderings

  1. Keerthi Kumar

    your solution is very useful but i want to know that how can i insert a new records(Am working on creating new events)

Add a Comment