I am trying to understand the @helper method for creating a nested list navigation. I need to assign a class to the current page to style it. Could someone look at my razor and give me some suggestions?
What is strange, we have another razor that uses a very similar helper running just fine. This is the other functioning helper. What is happening with my other script?
Okay, I've gotten it to work with the following code, by removing the .Children.Where("Visible") from the variable page, but I don't want to add the "childActive" class to the top level list items. Is there a way to write this to only change the class on only the nested list item that are "current page"?
I am new to some of the newances of C# or Razor, and would like it if you could explain why I have to include "int" before my parameters for "StartLevel" and "FinishLevel" in my @helper traverse(dynamic page, int StartLevel, int FinishLevel) ?
Aren't "StartLevel" and "FinishLevel", both parsed to integers when I assign the variables here?
var StartLevel = String.IsNullOrEmpty(Parameter.startLevel) ? 1 : int.Parse(Parameter.startLevel);
var FinishLevel = String.IsNullOrEmpty(Parameter.finishLevel) ? 3 : int.Parse(Parameter.finishLevel);
Here is my full Razor with the helper:
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var StartLevel = String.IsNullOrEmpty(Parameter.startLevel) ? 1 : int.Parse(Parameter.startLevel);
var FinishLevel = String.IsNullOrEmpty(Parameter.finishLevel) ? 3 : int.Parse(Parameter.finishLevel);
var page = @Model.AncestorOrSelf(StartLevel);
if(page != null)
{
@traverse(page, StartLevel, FinishLevel);
}
}
@helper traverse(dynamic page, int StartLevel, int FinishLevel)
{
<ul class="nodeList">
@foreach (var node in page.Children.Where("Visible"))
{
var itemName = node.HasValue("shortPageTitle") ? @node.shortPageTitle : node.HasValue("pageTitle") ? @node.pageTitle : @node.pageName;
var levelOne = node.IsAncestor(Model) ? "class=levelOne" : "";
var childActive = node.IsEqual(Model) ? "class=activeChild" : "";
var target = node.externalUrl ? "target=_blank" : "";
var link = node.HasValue("externalUrl") ? node.url : node.Url;
<li @childActive @levelOne ><a href="@link" @target>@itemName</a>
@if(node.Children.Where("Visible").Count() > 0 && node.IsAncestorOrSelf(Model) && node.Level <= FinishLevel)
{
@traverse(node, StartLevel, FinishLevel);
}
</li>
}
</ul>
}
@helper For Dynamic Nested List Navigation
I am trying to understand the @helper method for creating a nested list navigation. I need to assign a class to the current page to style it. Could someone look at my razor and give me some suggestions?
Hi Steve,
Is this not working?
Jeavon
The script doesn't load on the page, but it saves ok. So, there is something wrong with the way I've written it. Any ideas?
So it shows a error?
Error loading MacroEngine script (file: Category2LeftNav-RazorH.cshtml)
Here is the page: https://edit-wwwprep.rose-hulman.edu/offices-and-services/insurance-risk-management/student-insurance.aspx
Ok, try a catch or use Umbraco Debug Mode to see what the error is
Okay here is the resulting message:
'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'Children'
What is strange, we have another razor that uses a very similar helper running just fine. This is the other functioning helper. What is happening with my other script?
Okay, I've gotten it to work with the following code, by removing the .Children.Where("Visible") from the variable page, but I don't want to add the "childActive" class to the top level list items. Is there a way to write this to only change the class on only the nested list item that are "current page"?
Well, I figured it out myself. It was as simple as adding another ternary to the foreach.
Well done! I was just about to come back to you :-)
Jeavon,
I am new to some of the newances of C# or Razor, and would like it if you could explain why I have to include "int" before my parameters for "StartLevel" and "FinishLevel" in my @helper traverse(dynamic page, int StartLevel, int FinishLevel) ?
Aren't "StartLevel" and "FinishLevel", both parsed to integers when I assign the variables here?
Here is my full Razor with the helper:
is working on a reply...