Copied to clipboard

Flag this post as spam?

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


  • Jon Dunfee 199 posts 468 karma points
    Apr 14, 2014 @ 07:38
    Jon Dunfee
    0

    Using HasValue() with AncestorOrSelf() in MacroPartial

    Should be pretty self-explanatory of what I'm trying to do, but I'm getting definition not present [on HasValue] exception.  Could someone help me out to what the line "should" be.  Here's my last incarnation of trying different things:

    var pageWithAds = CurrentPage.AncestorOrSelf((Func<dynamic, bool>)(p => p.HasValue("rotationAds"))).FirstOrDefault();
  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Apr 14, 2014 @ 08:44
    Peter Gregory
    101

    I would go for the the strong typed version of this.

    var pagesWithAds = Model.Content.AncestorsOrSelf().FirstOrDefault(p => p.HasValue("rotationAds"));
    

    Im pretty sure that will give you what you want. It will return an IPublishedContent Item rather than Dynamic

    Peter

  • Jon Dunfee 199 posts 468 karma points
    Apr 14, 2014 @ 16:59
    Jon Dunfee
    0

    Thanks, Peter.  That did it and having typed is a bonus.  Here's my final code (unless someone sees a flaw):

        var pageWithAds = Model.Content.AncestorsOrSelf().FirstOrDefault(p => p.HasValue("rotationAds"));
        var rotationAds = ((pageWithAds != null) ? pageWithAds.GetPropertyValue<string>("rotationAds") : string.Empty);
        var ads = Umbraco.Media(rotationAds.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries));
        var adsPicked = (ads.Count() > 3) ? ads.Random(3) : ads;
    
  • 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