Copied to clipboard

Flag this post as spam?

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


  • Tom 161 posts 322 karma points
    Mar 24, 2015 @ 14:15
    Tom
    0

    How to reference a Parent Object in Razor

    I ahve a prent called view news and a partial view called articles

    I am using strongly typed code in my .cshtml file.

    In my partial view I have a foreach that iterates through the child items just fine.

    But I need to add a where condition to check a value from its parent.

    Question:  How do you refernce a parent object in a foreach where clause?

    Note:  The code bolded below is where I want to reference the parent object.


    Thanks


    Tom

    Here is my wehre clause now 

    @foreach (var page in Model.Content.Children.Where(a=>a.IsVisible()).Where(b=>b.GetPropertyValue<string>("category")==
    Model.Content.GetPropertyValue<string>"Category")).OrderByDescending(b=>b.CreateDate))

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 24, 2015 @ 14:19
    Dennis Aaen
    1

    Hi Tom.

    I am thinking that you can do something like this.

    @foreach (var page in Model.Content.Children.Where(a=>a.IsVisible()).Where(b => b.GetPropertyValue<string>("category") == Model.Content.Parent.GetPropertyValue<string>"Category")).OrderByDescending(b=>b.CreateDate))

    Hope this helps,

    /Dennis

  • Tom 161 posts 322 karma points
    Mar 24, 2015 @ 14:23
    Tom
    0

    Excellent! Many thanks!

  • Tom 161 posts 322 karma points
    Mar 24, 2015 @ 14:47
    Tom
    0

    Dennis:

     

    Spoke too soon.

    My query is returning no records.

    @foreach (var page in Model.Content.Children.Where(a => a.IsVisible()).Where(b => b.GetPropertyValue<string>("FHLBArticleType") == Model.Content.Parent.GetPropertyValue<string>("FHLBArticleType")).OrderByDescending(b => b.CreateDate))

    any idea why?

    Plus, how do you debug this code?

    Thanks

    Tom

  • Tom 161 posts 322 karma points
    Mar 24, 2015 @ 14:50
    Tom
    0

    This worked when I hardcoded in the value "News"

        @foreach (var page in Model.Content.Children.Where(a => a.IsVisible()).Where(b => b.GetPropertyValue<string>("FHLBArticleType") == "News").OrderByDescending(b => b.CreateDate))

  • Tom 161 posts 322 karma points
    Mar 24, 2015 @ 15:01
    Tom
    0

    Note:  Model.Content.Parent.GetPropertyValue<string>("FHLBCategory") == null.

    So how would I get the parent.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 24, 2015 @ 15:04
    Dennis Aaen
    0

    Hi Tom,

    Okay what if you do something like this:

    @foreach (var page in Model.Content.Children.Where(a=>a.IsVisible()).Where(b => b.GetPropertyValue<string>("category") == Model.Content.GetPropertyValue<string>("Category"),true)).OrderByDescending(b=>b.CreateDate))

    Hope this helps,

    /Dennis

  • Tom 161 posts 322 karma points
    Mar 24, 2015 @ 15:13
    Tom
    0

    That did it!  Many thanks.

    But why, how?

    How does the overloaded method call the parent?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 24, 2015 @ 15:18
    Dennis Aaen
    0

    Hi Tom,

    If you see the differece between this code

    @foreach(var page inModel.Content.Children.Where(a=>a.IsVisible()).Where(b => b.GetPropertyValue<string>("category")==Model.Content.Parent.GetPropertyValue<string>"Category")).OrderByDescending(b=>b.CreateDate))

    And this code

    @foreach(var page inModel.Content.Children.Where(a=>a.IsVisible()).Where(b => b.GetPropertyValue<string>("category")==Model.Content.GetPropertyValue<string>("Category"),true)).OrderByDescending(b=>b.CreateDate))

    Then you will notice that I removed the .Parent, and added , true. What the true parameter does is to look for the Category field recursively which means it will traverse the content trees ancestors until it find the field, and it has a value.

    Hope this make sense.

    /Dennis

  • Tom 161 posts 322 karma points
    Mar 24, 2015 @ 15:26
    Tom
    0

    Yes I see all of that but the documentation doesn't say or mention that it will traverse the parent!

    So many thanks for that.
    I would not have figured that out as I glossed over the word "Recursively" and didn't think it meant to traverse the parent.

    Here's the text from the documentation on my Umbraco 7.2 install. 

    Overloaded method 2 of 4 (extension) string IPublishedContent.GetPropertyValue(string alias, bool recursive)
    Recursively gets the value of a conent's property identified by its alias, converted to a specific type.  
    Recursive:: Avalue indicating whether to recurse. 

    Who do I contact to get them to fix the documentation?

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 24, 2015 @ 15:41
    Dennis Aaen
    1

    Hi Tom,

    You can sent, Umbraco an email here http://umbraco.com/contact/send-us-an-email, or if you are Twitter yourself, then many from Umbraco HQ that also are on Twitter

    Hope this helps,

    /Dennis

  • Tom 161 posts 322 karma points
    Mar 24, 2015 @ 18:21
    Tom
    0

    Thanks Dennis.  I'll try again tomorrow as the email site is down at the moment.

  • Paul Griffiths 370 posts 1021 karma points
    Feb 24, 2016 @ 19:18
    Paul Griffiths
    0

    Hi Dennis/Tom,

    Im trying to do something similar to this and im wondering if either of you can help please?

    On the home page I would like to only get a list of property listings where a property (called archiveAllProperties) on the tradingYear node (2016) is not set to true (bool dataType).

    I have the following structure

    > Home (doctype = homePage)
       > Properties (docType = properties)
           > 2016 (docType = tradingYear)
               > To Let (docType = propertyCategory)
                  > 11, New Hall Road (docType = propertyListing )
    

    From the home page i can get a list of all property listings by doing

    var propertyListings = siteRoot.Descendants("propertyListingPage").OrderBy("CreateDate desc");
    

    but how can only return all properties where the archiveAllProperties property on the 2016 tradingYear is set to true. I have been trying a where clause but not getting very far.

    This determines whether the archiveAllProperties is set to true or not

    var propertyListings = siteRoot.Descendants("tradingYearPage").Where("archiveAllProperties !=true")
    

    The idea behind this is that checking this archiveAllProperties property is a quick way to ignore all property listings for that trading year. There is of course going to be more than one year e.g. 2016, 2017, 2018 etc.

    Many thanks

    Paul

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 25, 2016 @ 19:35
    Dennis Aaen
    0

    Hi Paul,

    Could you try something like this, and see if it works like it should

    var propertyListings = siteRoot.Descendants("tradingYearPage").Where("archiveAllProperties == false")
    

    Hope this helps,

    /Dennis

  • Paul Griffiths 370 posts 1021 karma points
    Feb 26, 2016 @ 06:56
    Paul Griffiths
    0

    Hi Dennis

    thanks for the reply, I'll give that a whirl all though I did manage to resolve the issue using a couple of foreach loops.

    It might be a bit long winded but it does the job 👍

    Thanks Paul

  • 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