Copied to clipboard

Flag this post as spam?

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


  • Kate 267 posts 610 karma points
    Oct 04, 2012 @ 14:03
    Kate
    0

    submenu problem in cshtml file

    I have this menu that works almost fine. It givs me all the items <li></li> but I need it to not write the <ul> tag if there is now submenues. I will send you the code, maybe it will make more sense :-)

    I am total new to cshtml kode, I dont even know if the question belongs ind this kategorie. Hope you will help me anyway :-)


    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
        var level = String.IsNullOrEmpty(Parameter.Level) ? 2 : int.Parse(Parameter.Level);
        var parent = @Model.AncestorOrSelf(level);
       
        if (parent != null)
        {
            <ul class="subNavMenu">
            @foreach (var item in parent.Children)
            {
                var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : "";                       
                if (item.Visible)
                {               
                    <li@Html.Raw(selected)>
                        <a href="@item.Url">@item.Name</a>
                    </li>
                }
                else if(item.NodeTypeAlias == "MenuPunkt")
                {
                    var children = item.ChildrenAsList;

                    if (children.Count > 0)
                    {
                        var menuItem = children[0];
                        <li@Html.Raw(selected)>
                            <a href="@menuItem.Url">@item.Name</a>
                        </li>
                    }
                }
            }
            </ul>
        }
    }

  • Kasper Dyrvig 246 posts 379 karma points
    Oct 15, 2012 @ 12:05
    Kasper Dyrvig
    0

    Hi Kate,

    If you want the submenu to display childnodes from level 2 you should be able to reduce your razor code to this:

    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{ 
      var level = String.IsNullOrEmpty(Parameter.Level) ? 2 : int.Parse(Parameter.Level); 
      var parent = @Model.AncestorOrSelf(level);
      if (parent != null)
      {
      <ul class="subNavMenu">
        @foreach (var item in parent.Children)
        {
         var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
          if (item.Visible)
          {
            <li@Html.Raw(selected)>
             <a href="@item.Url">@item.Name</a>
            </li>
          }
        }
       </ul>
    }
    }

    ... Unless you have reson to check if the NodeTypeAlias is a "MenuPunkt". It depens on how you organize your content...?

Please Sign in or register to post replies

Write your reply to:

Draft