Adding class to menu UL

Topics: Writing themes
Jul 1, 2012 at 8:31 PM

I was looking to add a class to the ul for menu-main.  I copied the menu.cshtml from the Navigation views to my theme, and was expecting to find the ul, but rather found that the ul is inserted with Tag.StartElement, with the Tag class being created from the menu object:

@{
    // Model is Model.Menu from the layout (Layout.Menu)
    var tag = Tag(Model, "ul");
}
<nav id="main-menu" class="main-menu no-mobile">
    @tag.StartElement
    

After some poking around, I found that the classes are retrieved from the class passed into the Tag constructor, so I could achieve what I wanted with:

@{
  Model.Classes.Add("group-menu");
}

but modifying the model within the view seems rather un-MVC.

Is there a cleaner way to add a class to this tag?  I feel like I'm missing something.

Thanks

David

Developer
Jul 1, 2012 at 8:50 PM

Since the Tag property is of type OrchardTagBuilder which is derived from TagBuilder, I suppose you could add a class directly using its AddCssClass method.

 

 

 


Jul 3, 2012 at 9:00 PM

Thank you - that works