Keep menu active when sub-menu selected

Topics: Core
Oct 25, 2012 at 9:28 AM


I'm quite new to Orchard so please excuse me if I didn't find the response to that question. I've searched but didn't find something about it.

Here is my problem. I would like to keep a menu active for a group of pages. For exemple, if I create a query link named News that displays the list of news, I would like to have the News menu active when a news is displayed, not only active when the list is displayed.

But this could also apply to contenu menu items. For example the main menu is Products. It points to the first product. A submenu on the layer shows Product 1, Product 2, etc... I would like to keep the main menu Products active for all product pages.

I thought about using routes but didn't find how I can keep menu Products active for all paths that starts with /Products/*? Or is there a better way to achieve this?

Thanks in advance for the help

Oct 25, 2012 at 11:40 AM
Edited Oct 25, 2012 at 11:40 AM

As a complement, here is the solution I tried to implement unsuccessfully. My idea was to use the path. So I've added a field named MatchRoute to MenuPart. This field let me enter the path that should be matched to set the menu as active.

I've then overriden MenuItem.cshtml in my template to check if path match the given value and add the class accordingly. Unfortunately, I have not found how to access that field. I have not found a working example: some tutorials explain that I should be able to access the field with Model.Content.ContentItem.MenuPart.FieldName.Value. Unfortunately, I get an exception stating that ContentItem does not exist. Although I see the value in shape tracing tool. How can I access a custom field defined in MenuPart from MenuItem.cshtml view?

Oct 26, 2012 at 1:28 AM

Model.ContentItem.MenuPart.FieldName.Value, without the extra .Content at the beginning. But what is that field exactly? Doesn't the Model tab of shape tracing give you the code you need to put in your template?

Oct 26, 2012 at 7:47 AM
Edited Oct 26, 2012 at 8:05 AM

Thanks for your help. It's a custom field I've added to MenuPart. I want to be able to set a menu Active when the path contains a given string. So that field let me enter the path I want the menu to match. So I will for example set "/Products/" as value. Then in MenuItem.cshtml, I want to add the class "active" to the menu when the path contains /Products/.

I have tried with and without .Content at the beginning. If I include .Content at the beginning, I get the mentioned exception. If I don't include it, I don't get any exception, but the value is always null. What is strange is that shape tracing tool show me the value correctly (with .Content at the beginning). So I suppose I'm doing something wrong but can't find what.

So to resume my problem:

- I've added a custom text field (say MatchRoute) in MenuPart.
- In admin, I then have that additional field when editing menu items.
- I've copied MenuItem.cshtml file from Szmyd.Orchard.Modules.Menu\Views folder to my template View folder.
- I customized MenuItem.chtml to match my needs.
- I can't find how I can read the value set to MatchRoute field from the view.

Maybe there is a better way of doing what I try. I just want to be able to indicate that a menu should have the classe "active" when the path contains a given string. That string has to be editable in admin.

Oct 27, 2012 at 6:24 PM
Edited Oct 27, 2012 at 6:25 PM

Problem solved. I don't know how I missed that post during my searches: That's the reason why I'm getting the ContentItem exception.