I tried to fix the wiki page but wasn't able to save it for whatever reason. The snippet Top Navigation (using DynamicNode) is incorrect. I kept running my site and wondering why my IIS installation was crashing until I looked at what I pasted from that snippit. currentPage should be set to its parent and not Model.Parent otherwise if you go two levels deep it will crash your site. Can someone make that change to help out others.
Yeah, I've tried to correct that in the wiki as well, the umbraco team is working on fixing the wiki save problem! Don't forget the curlies on that same line by the way, they're required as of 4.7+
@{
var currentPage = Model;
while (currentPage.Level>2) { currentPage=currentPage.Parent; } // ** changed
<ul class="topnavigation">
@foreach (var c in currentPage.Children)
{
<li><a href="@c.Url">@c.Name</a></li>
}
</ul>
}
If i want to take into account the umbracoNaviHide property it would make sense to use .Where("umbracoNaviHide"), but since i need it to be false, how do i then negate that one? Tried various ways, but the result is alwys "can't cast bool to..."
In my menu (the new theme for 4.7) the current menu gets highlighted. This works like a treat with the code i have, but only on the first same level. If i move into an image album, the highlight disappears as the current page id isn't the same as the id's i run through in my foreach. I tried using the.Ancestors to travers to level 2 (my menu starting point), but to no avail.
<ul id="topNavigation"> @{ var level = 2; var currentPage = Model; while (@currentPage.Level > @level-1){ currentPage = @currentPage.Parent; }
Ah, finally the wiki is writeable and I could correct my mistake, plus make it look better. Missing properties are handled gracefully now, so this will not throw an error if one of the children is of a doctype without the umbracoNaviHide property.
I've a fix menu with link of level 2 node. Then when I click on one of them, must be appear the list of links to child of level 3 for that node of level 2
What is the exact error message that you're getting? Have you copied and pasted the code I provided exactly? It works perfectly for me.
Try and remove the .Where("umbracoNaviHide==false") and see if it works then.
If that does not work you're going to have to find out what exactly is null, so experiment with leaving bits of code out and see if the error still occurs.
So, what happens when you start commenting out code, at which line does it break? You could also set a breakpoint at the beginning and step through the code to see when it breaks.
Razor Snippet Top Navigation is Incorrect
I tried to fix the wiki page but wasn't able to save it for whatever reason. The snippet Top Navigation (using DynamicNode) is incorrect. I kept running my site and wondering why my IIS installation was crashing until I looked at what I pasted from that snippit. currentPage should be set to its parent and not Model.Parent otherwise if you go two levels deep it will crash your site. Can someone make that change to help out others.
Yeah, I've tried to correct that in the wiki as well, the umbraco team is working on fixing the wiki save problem! Don't forget the curlies on that same line by the way, they're required as of 4.7+
I have two questions for this one:
Hope you can point me in the right direction.
Regards
Allan
You could try something like this (not tested in any way and not very elegant either):
I am using this for the umbracoNaviHide
@foreach (var c in currentPage.Children)
{
if(c.umbracoNaviHide.ToString() == "1") {continue;}
<li><a href="@c.Url">@c.Name</a></li>
}
according to http://umbraco.com/follow-us/blog-archive/2011/3/1/umbraco-razor-feature-walkthrough-%E2%80%93-part-4 you can just do .Where("umbracoNaviHide != true") as per 4.7
Ah, finally the wiki is writeable and I could correct my mistake, plus make it look better. Missing properties are handled gracefully now, so this will not throw an error if one of the children is of a doctype without the umbracoNaviHide property.
@{
<ul class="topnavigation">
@foreach (var c in Model.AncestorOrSelf(1).Children.Where("umbracoNaviHide!=true")
{
<li><a href="@c.Url">@c.Name</a></li>
}
</ul>
}
Would be nice to add an HasAccess() also in the where - but that was not possible atm, could be added within the loop tho if (c.HasAccess())...
I just upgraded to RC and it works fine. The solution i ended up with is this:
I think it's pretty good, but if anyone has a better way let me know.
Very good! You could add this expression to shorten it:
I've runtime NULL error on this:
<ul id="topNavigation">
@{
if (@Model.Level>=2) {
var level = 3;
var currentMenu = @Model.AncestorOrSelf(@level);
foreach(var page in @Model.AncestorOrSelf(@level-1).Children.Where("umbracoNaviHide==false"))
{
if(@page.Id == @currentMenu.Id)
{
<li class="current">
<a class="navigation" href="@page.Url">@page.Name</a>
</li>
}
else
{
<li>
<a class="navigation" href="@page.Url">@page.Name</a>
</li>
}
}
}
}
</ul>
I wish to list first children of current node.
Thanks
You have too many @ signs, if you're in a code block (@{ .. }) you don't need to use @ unless you want to write something to the page:
same error...
My SiteMap is:
Umbraco Node Content ( level 0 )
IT - level 1
Node A ( level 2 )
Node A1 ( level 3 )
Node B ( level 2 )
I've a fix menu with link of level 2 node. Then when I click on one of them, must be appear the list of links to child of level 3 for that node of level 2
What is the exact error message that you're getting? Have you copied and pasted the code I provided exactly? It works perfectly for me.
Try and remove the .Where("umbracoNaviHide==false") and see if it works then.
If that does not work you're going to have to find out what exactly is null, so experiment with leaving bits of code out and see if the error still occurs.
Yes. I paste it as you wrote. I delete Where, but I've the same error Impossibile eseguire un'associazione di runtime su un riferimento Null
So, what happens when you start commenting out code, at which line does it break? You could also set a breakpoint at the beginning and step through the code to see when it breaks.
??? But I don't use Visual Studio, now. If I wish to use, how connect for debugging ?
Have a look at this blog post: http://blog.percipientstudios.com/2011/3/3/how-to-umbraco-razor-intellisense-and-debugging.aspx
You could also just start commenting out code until the error disappears.
var rootNodes = currentPage.AncestorsOrSelf.Last().Children;
<nav role="navigation" class="nav-collapse">
<ul class="nav">
@foreach(var rootNode in @rootNodes) {
bool hasChildren = rootNode.Children.Where("umbracoNaviHide != @0", "True").Any();
@Html.Raw(rootNode.Children.Where("umbracoNaviHide != @0", "true").Count().ToString())
<[email protected](hasChildren ? "class=\"dropdown\"" : "")>
@* Add CSS class selected if rootIsSelected *@
<a href="@rootNode.Url" class="@(currentPage.Id == rootNode.Id ? "selected" : "")">@rootNode.Name</a>
@* Display children on rootNode *@
@childPages(rootNode.Children, maxLevel)
</li>
}
</ul>
</nav>
@Html.Raw(rootNode.Children.Where("umbracoNaviHide != @0", "true").Count().ToString())
I have the code above.. why does the count always return 0... it doesn't seem like the where clause with umbracoNaviHide works at all...
is working on a reply...