I want to make sure my navigation has a page before looping. I know this can be solved with an if-statement but I don't know where in my code to put it
Here is my navigation code:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
int num = 1;
var rootNode = Model.Content.AncestorOrSelf(1);
// home node is hardcoded - this might not be right?
<ul id="menu">
<li>
<a href="/" class="drop">Hem</a>
</li><!-- End Home Item -->
@foreach (var mainNode in rootNode.Children())
{
int childCount = 1;
int numChildren = mainNode.Children().Count();
<li>
<a href="@mainNode.Url" class="drop">@mainNode.Name</a>
<div id=nav-@num class="dropdown_5columns">
<!-- Begin 2 columns container -->
<div class="col_5">
<h2>@mainNode.Name</h2>
</div>
@* note if you want ALL descendants change .Children to .Descendats*@
@foreach (var childNode in mainNode.Children().Where("Visible"))
{
// if first node or new set of three open the div and ul @: is used to stop razor trying to
// "balance" the tags
if (childCount == 1 || (double)childCount % 3 == 1)
{
@:<div class="col_1">
@:<ul>
}
<li><a href="@childNode.Url">@childNode.Name</a></li>
// close the div and list if this is either a multiple of 3 or the last one
if ((double)childCount % 3 == 0 || numChildren == childCount)
{
@:</ul>
@:</div>
}
childCount++;
}
</div>
</li>
num++;
}
</ul>
}
Hard to test without your content but you mean this?
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
int num = 1;
var rootNode = Model.Content.AncestorOrSelf(1);
// home node is hardcoded - this might not be right?
<ul id="menu">
<li>
<a href="/" class="drop">Home</a>
<div class="dropdown_2columns">
<!-- Begin 2 columns container -->
<div class="col_2">
<h2>Welcome !</h2>
</div>
</div><!-- End 2 columns container -->
</li><!-- End Home Item -->
@if (rootNode.Children().Any())
{
foreach (var mainNode in rootNode.Children())
{
int childCount = 1;
int numChildren = mainNode.Children().Count();
if(numChildren > 0)
{
<li>
<a href="@mainNode.Url" class="drop">@mainNode.Name</a>
<div id=nav-@num class="dropdown_5columns">
<!-- Begin 2 columns container -->
<div class="col_5">
<h2>@mainNode.Name</h2>
</div>
@* note if you want ALL descendants change .Children to .Descendats*@
@foreach (var childNode in mainNode.Children())
{
// if first node or new set of three open the div and ul @: is used to stop razor trying to
// "balance" the tags
if (childCount == 1 || (double)childCount % 3 == 1)
{
@:<div class="col_1">
@:<ul>
}
<a href="@childNode.Url">@childNode.Name</a>
// close the div and list if this is either a multiple of 3 or the last one
if ((double)childCount % 3 == 0 || numChildren == childCount)
{
@:
</ul>
@:
</div>
}
childCount++;
}
</div>
</li>
num++;
}
}
}
</ul>
}
Bah - it's done that tag balancing tag trick again.
Try:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
int num = 1;
var rootNode = Model.Content.AncestorOrSelf(1);
// home node is hardcoded - this might not be right?
<ul id="menu">
<li>
<a href="/" class="drop">Home</a>
<div class="dropdown_2columns">
<!-- Begin 2 columns container -->
<div class="col_2">
<h2>Welcome !</h2>
</div>
</div><!-- End 2 columns container -->
</li><!-- End Home Item -->
@if (rootNode.Children().Any())
{
foreach (var mainNode in rootNode.Children())
{
int childCount = 1;
int numChildren = mainNode.Children().Count();
if(numChildren > 0)
{
<li>
<a href="@mainNode.Url" class="drop">@mainNode.Name</a>
<div id=nav-@num class="dropdown_5columns">
<!-- Begin 2 columns container -->
<div class="col_5">
<h2>@mainNode.Name</h2>
</div>
@* note if you want ALL descendants change .Children to .Descendats*@
@foreach (var childNode in mainNode.Children())
{
// if first node or new set of three open the div and ul @: is used to stop razor trying to
// "balance" the tags
if (childCount == 1 || (double)childCount % 3 == 1)
{
@:<div class="col_1">
@:<ul>
}
<a href="@childNode.Url">@childNode.Name</a>
// close the div and list if this is either a multiple of 3 or the last one
if ((double)childCount % 3 == 0 || numChildren == childCount)
{
@:</ul>
@:</div>
}
childCount++;
}
</div>
</li>
num++;
}
}
}
</ul>
}
Add .Where("Visible") whereever it checks for Children.
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
int num = 1;
var rootNode = Model.Content.AncestorOrSelf(1);
// home node is hardcoded - this might not be right?
<ul id="menu">
<li>
<a href="/" class="drop">Home</a>
<div class="dropdown_2columns">
<!-- Begin 2 columns container -->
<div class="col_2">
<h2>Welcome !</h2>
</div>
</div><!-- End 2 columns container -->
</li><!-- End Home Item -->
@if (rootNode.Children().Where("Visible").Any())
{
foreach (var mainNode in rootNode.Children().Where("Visible"))
{
int childCount = 1;
int numChildren = mainNode.Children().Where("Visible").Count();
if(numChildren > 0)
{
<li>
<a href="@mainNode.Url" class="drop">@mainNode.Name</a>
<div id=nav-@num class="dropdown_5columns">
<!-- Begin 2 columns container -->
<div class="col_5">
<h2>@mainNode.Name</h2>
</div>
@* note if you want ALL descendants change .Children to .Descendats*@
@foreach (var childNode in mainNode.Children().Where("Visible"))
{
// if first node or new set of three open the div and ul @: is used to stop razor trying to
// "balance" the tags
if (childCount == 1 || (double)childCount % 3 == 1)
{
@:<div class="col_1">
@:<ul>
}
<a href="@childNode.Url">@childNode.Name</a>
// close the div and list if this is either a multiple of 3 or the last one
if ((double)childCount % 3 == 0 || numChildren == childCount)
{
@:</ul>
@:</div>
}
childCount++;
}
</div>
</li>
num++;
}
}
}
</ul>
}
Misunderstood your first question. Just took out the first if..
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
int num = 1;
var rootNode = Model.Content.AncestorOrSelf(1);
// home node is hardcoded - this might not be right?
<ul id="menu">
<li>
<a href="/" class="drop">Home</a>
<div class="dropdown_2columns">
<!-- Begin 2 columns container -->
<div class="col_2">
<h2>Welcome !</h2>
</div>
</div><!-- End 2 columns container -->
</li><!-- End Home Item -->
foreach (var mainNode in rootNode.Children().Where("Visible"))
{
int childCount = 1;
int numChildren = mainNode.Children().Where("Visible").Count();
if(numChildren > 0)
{
<li>
<a href="@mainNode.Url" class="drop">@mainNode.Name</a>
<div id=nav-@num class="dropdown_5columns">
<!-- Begin 2 columns container -->
<div class="col_5">
<h2>@mainNode.Name</h2>
</div>
@* note if you want ALL descendants change .Children to .Descendats*@
@foreach (var childNode in mainNode.Children().Where("Visible"))
{
// if first node or new set of three open the div and ul @: is used to stop razor trying to
// "balance" the tags
if (childCount == 1 || (double)childCount % 3 == 1)
{
@:<div class="col_1">
@:<ul>
}
<a href="@childNode.Url">@childNode.Name</a>
// close the div and list if this is either a multiple of 3 or the last one
if ((double)childCount % 3 == 0 || numChildren == childCount)
{
@:</ul>
@:</div>
}
childCount++;
}
</div>
</li>
num++;
}
}
</ul>
}
Making sure the navigation has pages
I want to make sure my navigation has a page before looping. I know this can be solved with an if-statement but I don't know where in my code to put it
Here is my navigation code:
Hi,
Hard to test without your content but you mean this?
No, that didn't work.
Here is my content:
What's the exact problem / error!
What page are you on when you see the error - is it all?
I get error:
Bah - it's done that tag balancing tag trick again.
Try:
Did you know if you go to the log (stored in \App_Data\Logs\UmbracoTraceLog.txt) you can see the last error. This helps fixing these sort of things.
Steve
I don't get an error now but it didnt solve my problem.
I notice that you removed the
.Where("Visible")
. That's bad because there are some some items that I want to hide from the menu and now they're back.Is it possible to check whether the drop-down doesn't have childNodes(or has them hidden from the menu)?
If a parent doesn't have children or if they're hidden, not running the loop?
You've pretty much answered your own question.
Add .Where("Visible") whereever it checks for Children.
Hmm.. Now it removed the headmenu from the navigation. I want it to remove the drop-down, not the MainMenu
Before:
Now:
Misunderstood your first question. Just took out the first if..
is working on a reply...