Copied to clipboard

Flag this post as spam?

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


  • Shawty 3 posts 22 karma points
    May 27, 2013 @ 18:02
    Shawty
    0

    Traversing up the tree for a default property

    Hi all, 

    This may have been asked before, but after spending several hours combing here (and arguing with moderation nazis on stackoverflow) Iv'e decided to join and ask my question here :-)

    For reference, I'm fairly new to Umbraco, but in no way am I new to .NET/Razor/C# and software development in general (30+ years exp) , point being, I'm a newbie but not a newbie...  if that makes sense :-)

     

    ANYWAY....

     

    Upto now I've been using this pattern to pass parameters from my page properties in the content editor into my razor macros:

    @Umbraco.RenderMacro(
      "TopMenu", new { 
         MenuIdOne = @Umbraco.Field("menuPageOne", altText: "0000", recursive: true), MenuNameOne = @Umbraco.Field("menuNameOne", altText: "", recursive: true),
        MenuIdTwo = @Umbraco.Field("menuPageTwo", altText: "0000", recursive: true), MenuNameTwo = @Umbraco.Field("menuNameTwo", altText: "", recursive: true),
        MenuIdThree = @Umbraco.Field("menuPageThree", altText: "0000", recursive: true), MenuNameThree = @Umbraco.Field("menuNameThree", altText: "", recursive: true),
        MenuIdFour = @Umbraco.Field("menuPageFour", altText: "0000", recursive: true), MenuNameFour = @Umbraco.Field("menuNameFour", altText: "", recursive: true),
        MenuIdFive = @Umbraco.Field("menuPageFive", altText: "0000", recursive: true), MenuNameFive = @Umbraco.Field("menuNameFive", altText: "", recursive: true)
    })

    Embedding these calls directly into my Master pages and views worked well so far, esp given that I got the recursive stuff for free as well as a default value.

    In my razor macro I was then able to check these values for the defaults and such like, and not have to worry about doing any traversing or anything like that myself.

    I'm now using uComponents (Which rocks BTW), but when using the Url Picker I can't pass them into my macro in the way I have been (at Least not easily anyway), so I've had to resort to using the more accepted Model binding method.

    EG:

    if we imagine I have a pageProperty called 'socialMediaLink' then in my razor macro I use '@Model.socialMediaLink' to get the object, rather than passing it in using a parameter.

    Now that's all fine and dandy, works perfectly, my code's much cleaner and easier to read.  No problems there.

    However, beacuse I'm now not using the razor macro method I've lost the "free" recursiveness in the page tree that I was taking for granted, and am now having a hard time figuring out how to do this in razor, in my macro cshtml file.

    More specifically, I get the DynamicNode thing (even if it is a bit maddening sometimes) and I know that we have:

    .HasProperty()
    .HasValue()

    and all those other good things, but I can't seem to figure out how to make them work with the "Where" function.

    Being a LINQ/Razor veteran in general, the first thing I tried was to use a Lambda, and then it struck me (Like a ton of bricks I might add) that this is a dynamic object so no I can't use Lambda's....

    Then I looked through other examples, and saw how to use Ancestors with Where, and that the Where parameter was a string, so my final attempt before posting this was something like the following:

    var recursedProperty = Model.Ancestors().Where("HasValue(\"socialMediaIconOne\")")

    only to get an exception telling me "NO" :-)

    so after my novel above, my actual question is this...

    If I have a base document type, that ALL my other document types inherit from, and IF a property in one of those inherited documents is NOT set, then using razor, in a cshtml macro, what is the best way to climb the page tree looking for a value for said property?

    Ideally, I'd like to be able to do it using LINQ as I tried above, and not half a dozen nested loops (The reason for this is I'm checking 5 properties, so don't really want 5 massive loops for each one)

    for the record, I did also try the above using an "!= null" clause, but since my property always exists (It just doesnt always have a value) then checking for null wasn't helpfull.

    in case it's needed, this is my Umbraco build version:

    umbraco v 4.11.8 (Assembly version: 1.0.4869.17899)

    I hope my question makes sense, I'd much rather stay here than go back to Stackoverflow (Which is the pit's these days, just full of whiney little reputation hunters) :-)

    Shawty

    (and yes if anyone asks, and anyone recognises the name...  I am Shawty from Lidnug .... )


  • Shawty 3 posts 22 karma points
    May 27, 2013 @ 18:05
    Shawty
    0

    Oh a bit of an adendum, sorry forgot to add this:

    .NET version is Version 4

    Umbraco is running under a full blown IIS7.5 instance on Windows Webserver 2008 and I'm working accross a genuine network link, not on a localhost installation.

     

  • Shawty 3 posts 22 karma points
    May 27, 2013 @ 21:10
    Shawty
    0

    ha, and as is usually the case.... I post it, then a little while later I solve it.

    Turns out I didn't know about the ability to access property names prefixed with an 

    _  (underscore)

    and a lowercase first letter.

    More's the point turns out I didn't know that performing the access that way was recursive. 

    Anyway, my initial question is now solved, but I still have one small mystery that's related.

    My properties consist of one Media object (Representing a social media Icon) and one social media url (Representing the link to said social media)

    I'm using a standard Media Picker for the icon, and I'm using uComponents for the UrlPicker, the standard media picker recurses perfectly fine, but the UrlPicker does not.

    Using .HasValue reports that the UrlPicker has a value in the inherited pages, but when trying to access the Url & Title properties the values are empty.

     

Please Sign in or register to post replies

Write your reply to:

Draft