Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Craig100 1136 posts 2523 karma points c-trib
    Oct 15, 2011 @ 19:11
    Craig100
    0

    Top menu active from side menu

    Hi,

    I have the following topNavigation.cshtml which works nicely for top level pages, keeping the main-active class applied to the current menu item.

    <ul>
     @{
       string style="";
       }
    
     @if(Model.Name=="Welcome"){style = " class=\"main-active\"";}
    
      <li @Html.Raw(style)>
        <a href="/Welcome" title="Welcome">Welcome</a>
      </li>
    
    @foreach (var page in Model.AncestorOrSelf(1).Children.Where("Visible"))
    {
    style="";
    
    if(([email protected])){style = " class=\"main-active\"";}
    
      <li @Html.Raw(style)>
        <a href="@page.Url" title="@page.Name">
          @page.Name
        </a>
      </li>  
    }
    </ul>

    I want to add something in or around here

    if(([email protected])){style = " class=\"main-active\"";}

    to cause the top menu item to remain highlighted when a child page is displayed as well. I can't get my head around it. Can anyone suggest something?

    Regards,

    Craig

     

  • Anthony pj 40 posts 63 karma points
    Oct 16, 2011 @ 00:36
    Anthony pj
    1

    This will work ................

    Replace your foreach loop with the following ...........

          
        @foreach (var item in Model.AncestorOrSelf(1).Children.Where("Visible")) {
          var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"main-active;\"" : "";
          <li @Html.Raw(selected)>
            <a href="@item.Url">@item.Name</a>
          </li>
          }
       

  • Anthony pj 40 posts 63 karma points
    Oct 16, 2011 @ 01:35
    Anthony pj
    1

     You could also use
        
     @foreach (var item in Model.AncestorOrSelf(1).Children.Where("Visible")) {
        
          <li class="@Model.IsDescendantOrSelf(item,"main", "")">
            <a href="@item.Url" >@item.Name</a>
          </li>
          }
           

     

       

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies