Navigation Using a Consistent Value for The Top Level Node
I am trying to use the same navigation on all children of a node within a site using razor. The problem is that I don't know how to write the code so that even the children will have all the same navigation that the top most node would have. @Model (current node) changes acording to which page you are on and then the children no longer have the same top level node descendants, so no navigation is displayed. I hope that is understandable. Here is my code:
@{
var listings = Model.DescendantsOrSelf(); var listGroup = listings.Count()/3;
Will try to help all I can, but sorry don't use webforms so can only advise, but believe me I have been where you are many times.
My first thought is to simplify, strip out the if statements, just get the call right, from memory, you may need descendants rather than children and without seeing your tree its a bit of guesswork. Could also try AncestorOrSelf (ie leave out the "s")
Just thought that I have a live site in 4.7 which is in webforms although I just direct straight out to a cshtml file, but I will have a look in there to see what I can find.
Will have a look now and see if I can find something to help - been working with InDesign all day, will be a relief.
Now you say it, yes, my understanding is that Children is only on static, but you need descendants for dynamic (or something similar), and yes, have had to split them in the past.
A lot of the "razor walkthrough" series is still valid, as is the razor node cheat sheet (as far as I am aware), usually refer to these if seeking a bit of inspiration.
Navigation Using a Consistent Value for The Top Level Node
I am trying to use the same navigation on all children of a node within a site using razor. The problem is that I don't know how to write the code so that even the children will have all the same navigation that the top most node would have. @Model (current node) changes acording to which page you are on and then the children no longer have the same top level node descendants, so no navigation is displayed. I hope that is understandable. Here is my code:
@{
var listings = Model.DescendantsOrSelf();
var listGroup = listings.Count()/3;
<div class="header-nav">
@foreach (var group in listings.InGroupsOf(@listGroup)) {
<ul class="column">
@foreach (var item in group) {
<li><a href="@if ( item.url != "") { @item.url; } else { @item.Url; }">
@if (item.HasValue("shortPageTitle") ) {
@item.shortPageTitle;
} else if (item.HasValue("pageTitle") ) {
@item.pageTitle;
} else {
@item.Name;
}
</a></li>
}
</ul>
}
</div>
}
Why doesn't this work to start my navigation at the node "CourseCatalogHome"?
var listings = Model.Descendants().HasProperty("CourseCatalogHome");
Hi Steve
Without knowing which version you are using, the caode maybe different.
This will work with Mvc as opposed to webforms, but the theory is the same.
To get to root node;
var root= CurrentPage.AncestorOrSelf() Will climb to to top node in the tree, thus making navigation equal on each page.
then foreach ( var item in root.Children("Add your child doctype alias"))
or you can put it all together as var root = CurrentPage.AncestorOrSelf().Children("Add your child doctype");
foreach (var item in root)
As I said it maybe slightly different in webforms, but if you follow the theory you should get there.
Hope it helps
Best regards
Gary
Gary,
I have tried what you suggested with no results. What am I missing? I've been on this for hours.
Steve
HI
Just checked, both used @Model.AncestorOrSelf().Children() so it is the singular Ancestor and Children.
One other thought is that if your parent node is not the top level, try AncestorOrSelf(1).Children().
Check your docType name if camel case, ie needs to be courseCatalog . . . . the first letter will not be a capital, this has caught me in the past.
Sorry I can't be more specific, but hope something will help.
Gary
Got it. Thanks for your help! I had to seperate root from the Descendants to get it to work. Don't know why.
Cool!
Hi gary/steve,
can you experts please help with a very small thing,
I'm new to this and just wanted to know "the number of levels of navigation" (from root level) by using some method or function
I tried "Model.Descendants(1).Last()" but that resulted in getting the last node of my site, but I want the final level (deepest level)
please visit this link to understand my query clearly
http://our.umbraco.org/forum/developers/razor/38566-how-to-get-the-deepest-level-child-of-Home-page
thanks
Roey
is working on a reply...