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
I have a navigation helper which checks the id of the current page and compares it to the navigation item being rendered and then gives a class of selected if it returns true.
for example
@helper SelectedStyle(dynamic page) { if (CurrentPage.Id == page.Id || CurrentPage.AncestorOrSelf(2).Id == page.Id || CurrentPage.AncestorOrSelf(3).Id == page.Id) { @Html.Raw("class=\"selected\""); }}
The trouble is it errors on top level nodes which dont have children that go three levels deep.
the error is:
because there is no 3rd level on that particular node.
how do I get it to fail silently?I've tried all sorts like
if(String.IsNullOrEmpty(CurrentPage.AncestorOrSelf(3).Id) == false)
String.IsNullOrEmpty(
CurrentPage.AncestorOrSelf(3).Id) == false)
and
if(CurrentPage.AncestorOrSelf(3).Id = null)
CurrentPage.AncestorOrSelf(3).Id = null)
but everything I try seems to fall flat.
what is the correct way to see if CurrentPage.AncestorOrSelf(3) returns null or not?
- Tim
Ok, thought I tried this before but it turns out a simple
if (CurrentPage.AncestorOrSelf(3) != null)
Works...
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
how to check if AncestorOrSelf Returns null?
I have a navigation helper which checks the id of the current page and compares it to the navigation item being rendered and then gives a class of selected if it returns true.
for example
The trouble is it errors on top level nodes which dont have children that go three levels deep.
the error is:
Cannot perform runtime binding on a null reference
because there is no 3rd level on that particular node.
how do I get it to fail silently?
I've tried all sorts like
and
but everything I try seems to fall flat.
what is the correct way to see if CurrentPage.AncestorOrSelf(3) returns null or not?
- Tim
Ok, thought I tried this before but it turns out a simple
Works...
is working on a reply...