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.
<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.
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) ; } }
@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!
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.
This should work:
But, presumably you also want the products to display when you click on a product, so change the foreach to this:
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. :-)
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.
@Sebastiaan off-topis: how do I paste code in blocks as in your post?
Thnx barry and Sebastiaan, Though Sebastian post works too but bary answer fits my requirement
@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!
is working on a reply...