Copied to clipboard

Flag this post as spam?

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


  • Mr A 216 posts 278 karma points
    Sep 16, 2011 @ 15:58
    Mr A
    0

    2nd Level Navigation

    My tree structure is :

    HomePage

    Products

    Product 1

    Product 2

    Product 3

    Testimonials

    Contact Us

    I just want to show the product navigation on one of my page , for instance if i click on product the product page should be displayed with all the products listing in the side menu.

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Sep 16, 2011 @ 16:02
    Sebastiaan Janssen
    0

    This should work: 

    <ul>
    @foreach(var node in Model.Children) {
     <li><a href="@node.Url">@node.Name</li>

    </ul> 

    But, presumably you also want the products to display when you click on a product, so change the foreach to this:

    @foreach(var node in Model.AncestorOrSelf(1).Children) 

    This will go and look at the current node, if it's "Products" then you're at level 1 (which is what the 1 is for), if not, it will look at the ancestor and see if that is level 1. Then it lists the children.

    Hope that makes sense. :-)

  • Barry Fogarty 493 posts 1129 karma points
    Sep 17, 2011 @ 15:19
    Barry Fogarty
    1

    Hi Mr A, welcome to the forum.  Sebastiaan's soltion is the simplest, and seems to fit the bill when you only want to show nodes at the same level on any given page. However if you are looking for a nested menu, so you can display a number of levels (more like a 'tree' menu, you will need a nested for loop like so:

    @{
      var startLevel = 1;
      var finishLevel = 2;  
      var parent = @Model.AncestorOrSelf(startLevel);
      if (parent != null) { @ulMenu(parent, startLevel, finishLevel) ; }
    }

    @helper ulMenu(dynamic parent, int startLevel, int finishLevel)
    {
     <ul @Html.Raw(ulClass)>
        @foreach (var node in parent.Children.Where("Visible")) {
            <li>
                <a href="@node.Url">@node.Name</a>                                      
                @if (@node.Level <= finishLevel) { @ulMenu(node, startLevel, finishLevel); } 
            </li>
        }
     </ul>                                                           
    }

    This code is from the razor snippets section, hopefully it is of some use.

  • Barry Fogarty 493 posts 1129 karma points
    Sep 17, 2011 @ 15:22
    Barry Fogarty
    0

    @Sebastiaan off-topis:  how do I paste code in blocks as in your post?

  • Mr A 216 posts 278 karma points
    Sep 19, 2011 @ 12:27
    Mr A
    0

    Thnx barry and Sebastiaan, Though Sebastian post works too but bary answer fits my requirement

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Sep 19, 2011 @ 12:59
    Sebastiaan Janssen
    0

    @barry in the dropdown where it says "Paragraph", select "Preformatted" instead. It takes a bit of playing and using shift+enter sometimes to get some nice looking output but it works.

    @Mr A: what do you mean? You didn't ask for multilevel, you asked for one level! ;-) Just kidding, Barry's solution is good!

Please Sign in or register to post replies

Write your reply to:

Draft