Search code examples
razorumbracoumbraco7

Umbraco 7.12: If X Matches Current Page ID, OR Any of Current Page's Descendants - is it possible?


got this snippet ive put together:

@foreach(var item in selection){
    if(item.HasValue("location") && @Model.Content.Id == @item.GetPropertyValue<int>("location")){
var singleLink = item.GetPropertyValue<Link>("links");
    if(singleLink != null)
    {
        if(item.HasValue("wideImage"))
            {  
            IPublishedContent mediaItem = item.GetPropertyValue<IPublishedContent>("wideImage");
               <div class="owl-item" style="width: 891px;"><a href="@singleLink.Url" target="@singleLink.Target"><img class="tag-headadvert img-responsive" style="display: inline;" src="@mediaItem.Url" width="100%" alt="@singleLink.Name" /></a></div> 
            }
    }
}

In the second line you can see I make a comparison of the current page ID, vs a variable, is there a way to do, if that variable matches the current page ID, or any descendants of the current page ID?

Thanks


Solution

  • You would need to retrieve the list of Descendant Ids - one of the easiest ways would be to select the Id's from Model.Content.DescendantsOrSelf() - e.g.:

    var descendantIds = Model.Content.DescendantsOrSelf().Select(c => c.Id);
    

    Then you can use descendantIds.Contains(@item.GetPropertyValue<int>("location")) in your second line.

    Note: if you have a large number of descendants to the current node, you may find you will need to somehow optimise the query, perhaps using caching or come up with an entirely different way of achieving the result you want.