Martin Overgaard
Martin Overgaard

Reputation: 355

Current link in top and submenu

I'm trying to set a "current" class on both top and sub menu items in an Umbraco installation.

Topmenu is like :

Home Products About Contact

Now when I click let's say Products, then i set the class name to "current". At the same time it loads a submenu like this :

Jeans
Sweeters
T-shirts
- Red
- Green
- Blue

And when I click let's say Sweeters then I wan't the products link and the sweeters link to have the the current class. How can I do this?

Code for topmenu

@{
    <ul class="topnavigation">
    @foreach (var c in Model.AncestorOrSelf(2).Children.Where("umbracoNaviHide!=true"))
    {
        <li class="@(Model.Id == c.Id ? "current" : "")"><a href="@c.Url">@c.Name</a></li>
    }
    </ul>
}

Code for submenu

@{
<ul>
    @foreach (var page in @Model.AncestorOrSelf(3).Children)
    {
        string style = string.Empty;
        if (Model.Id == page.Id) { style = "class=current"; }
        <li @style><a href="@page.Url" @Html.Raw(style)>@page.Name</a></li>
            if (page.Childen != null && page.Children.Count() > 0 && Model.AncestorsOrSelf().Where("Id == @0", page.Id).Count() > 0)
            {                
                <ul>
                    @foreach (dynamic secondPage in page.Children.Where("!umbracoNaviHide"))
                    {    
                        string style1 = string.Empty;
                        if (Model.Id == secondPage.Id) { style1 = "class=current"; }                
                        <li @style1>
                            - <a href="@secondPage.Url">@secondPage.Name</a>
                        </li>
                    }
                </ul>
            }
    }
</ul>
}

Upvotes: 1

Views: 954

Answers (1)

Digbyswift
Digbyswift

Reputation: 10400

A page has a Path property which contains a comma-delimited list of IDs representing the ancestors of the page.

You could check whether the Products page's ID exists within the current page's Path property using something like @Model.Path.Contains(c.Id.ToString()).

Upvotes: 1

Related Questions