Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • wolulcmit 357 posts 693 karma points
    Aug 07, 2013 @ 11:15
    wolulcmit
    0

    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

    @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:

    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

    if(String.IsNullOrEmpty(CurrentPage.AncestorOrSelf(3).Id) == false)

    and

    if(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

  • wolulcmit 357 posts 693 karma points
    Aug 07, 2013 @ 12:45
    wolulcmit
    100

    Ok, thought I tried this before but it turns out a simple

    if (CurrentPage.AncestorOrSelf(3) != null)

    Works...

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies