Copied to clipboard

Flag this post as spam?

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


  • Balram Narad 37 posts 89 karma points
    May 09, 2014 @ 12:08
    Balram Narad
    0

    Banner Image won't work recursively

    HI

    I'm new to umbraco and razor and am trying to get an banner image to display on each page.

    The banner image should be the banner image on the page if it has one otherwise should be the next banner image it finds up the content tree.

    E.g

    Homepage  - has banner

    Section One -has no banner so should pick up from homepage

    Section Two - has banner should display it's own

       subsection two - has no banner should pick up banner from Section Two not homepage.

    Section Three - has no banner should pick up from homepage

      subsection three - has no banner should pick up from Homepage

    etc

    The code I  have so far is

    @{

           var bannerImage = Model.Content.AncestorsOrSelf().Where(x => x.HasProperty("bannerImage") && x.HasValue("bannerImage")).FirstOrDefault();

        if (bannerImage != null){

            <img class="banner" src="@(bannerImage.GetPropertyValue<DAMP.PropertyEditorValueConverter.Model>("bannerImage").First.Url)"/>

        }

       }

    but this either gives me the first banner image on every page

     

    Any help much appreciated

     

    Bal

  • MK 429 posts 906 karma points
    May 09, 2014 @ 12:48
    MK
    0

    Hi,

    You can create a recursive method, something like this:

    @functions{//To place in .cshtml

    public static string GetPath(int nodeId)

    {

       var m = uQuery.GetNode(nodeId);

        string res = string.Empty;

        if (true)//check if has property and value

    {

    res = "";//n.prop.value;

    }else

    {//call it again and send its parent id

          res =  GetPath(m.Parent.Id);

    }

        return res;

    }

    }

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 11, 2014 @ 02:15
    Jeavon Leopold
    0

    Hi Balram,

    I wonder, could you try the following?

       var bannerImage = Model.Content.AncestorOrSelf().Where(x => x.HasProperty("bannerImage") && x.HasValue("bannerImage"));
    

    Jeavon

  • Balram Narad 37 posts 89 karma points
    May 12, 2014 @ 10:31
    Balram Narad
    0

    Hi Jeavon

    That's done the trick thanks very much

    Bal

Please Sign in or register to post replies

Write your reply to:

Draft