Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi guys,
Quick question: I made a menu by using Traversing. Now i want to add a @if statement to add Home to the first level (nodelevel 2) of children.
I have this code but i want to know where i go wrong:
@using Our.Umbraco.Vorto.Extensions @inherits Umbraco.Web.Macros.PartialViewMacroPage @{ var selection = CurrentPage.Site(); } @Traverse(selection) @helper Traverse(dynamic node){ var maxLevelForSitemap = 4; var selection = node.Children.Where("menuweergave == true").Where("Level <= " + maxLevelForSitemap); if (selection.Any()) { <ul> @if(selection.Level == 2){ <li class="menu_li_2"><a href="/">Home</a> } @foreach (var item in selection.Where("menuweergave == true")){ IPublishedContent sitem= (IPublishedContent)item; <li class="[email protected]"><a href="@item.Url">@sitem.GetVortoValue("paginatitel")</a> @Traverse(item) </li> } </ul> } }
Something goes wrong with selection.Level but i can't figure out what.
Hi Lex,
It seems like the issue would be that you are not closing the <li> element at the end of your Home link. This will break your @if statement and cause your helper to fail.
<li>
@if
Try adding the </li> to the end of your link and see if that helps.
</li>
Gary
That must have been a mistake whilst 'Ctrl + Y'-ing. Fixed it but still gives an error.
Would you mind sharing the error that you are receiving? This will help me debug.
I don't think it would help much but i get a Error loading Partial View script (file: ~/Views/MacroPartials/Menu.cshtml).
Error loading Partial View script (file: ~/Views/MacroPartials/Menu.cshtml)
Is there any way to get a function that checks your PVMF on error's like for the XSLT-files?
Edit: Thanks for your help btw
I just noticed that you are using selection.Level where selection is of type IEnumerable<IPublishedContent>.
selection.Level
IEnumerable<IPublishedContent>
Try moving your @if statement inside your foreach loop and change selection.Level to item.Level
foreach
item.Level
Hopefully that solves your issue!
Thanks for your response Gary, you helped me figure it out:
@using Our.Umbraco.Vorto.Extensions @inherits Umbraco.Web.Macros.PartialViewMacroPage @{ var selection = CurrentPage.Site(); } @Traverse(selection) @helper Traverse(dynamic node){ var maxLevelForSitemap = 4; var selection = node.Children.Where("menuweergave == true").Where("Level <= " + maxLevelForSitemap); var selectionLevel = node.Level; if (selection.Any()) { <ul> @if(selectionLevel == 1){ <li class="menu_li_2"><a href="/">Home</a></li> } @foreach (var item in selection.Where("menuweergave == true")){ IPublishedContent sitem= (IPublishedContent)item; <li class="[email protected]"><a href="@item.Url">@sitem.GetVortoValue("paginatitel")</a> @Traverse(item) </li> } </ul> } }
I added another variable called selectionLevel to get the node.Levelvar selectionLevel = node.Level;. Then i addd this if statement:
var selectionLevel = node.Level;
@if(selectionLevel == 1){ <li class="menu_li_2"><a href="/">Home</a></li> }
I couldnt use your suggestion because it would add another <li>home</li> to each var item. But it helped me realise what i was doing wrong, thanks!
<li>home</li>
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Traversing menu - adding Home
Hi guys,
Quick question: I made a menu by using Traversing. Now i want to add a @if statement to add Home to the first level (nodelevel 2) of children.
I have this code but i want to know where i go wrong:
Something goes wrong with selection.Level but i can't figure out what.
Hi Lex,
It seems like the issue would be that you are not closing the
<li>
element at the end of your Home link. This will break your@if
statement and cause your helper to fail.Try adding the
</li>
to the end of your link and see if that helps.Gary
That must have been a mistake whilst 'Ctrl + Y'-ing. Fixed it but still gives an error.
Would you mind sharing the error that you are receiving? This will help me debug.
I don't think it would help much but i get a
Error loading Partial View script (file: ~/Views/MacroPartials/Menu.cshtml)
.Is there any way to get a function that checks your PVMF on error's like for the XSLT-files?
Edit: Thanks for your help btw
Hi Lex,
I just noticed that you are using
selection.Level
where selection is of typeIEnumerable<IPublishedContent>
.Try moving your
@if
statement inside yourforeach
loop and changeselection.Level
toitem.Level
Hopefully that solves your issue!
Gary
Thanks for your response Gary, you helped me figure it out:
I added another variable called selectionLevel to get the node.Level
var selectionLevel = node.Level;
. Then i addd this if statement:I couldnt use your suggestion because it would add another
<li>home</li>
to each var item. But it helped me realise what i was doing wrong, thanks!is working on a reply...