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.
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?
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.
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))
Hi Tom.
I am thinking that you can do something like this.
Hope this helps,
/Dennis
Excellent! Many thanks!
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
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))
Note: Model.Content.Parent.GetPropertyValue<string>("FHLBCategory") == null.
So how would I get the parent.
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
That did it! Many thanks.
But why, how?
How does the overloaded method call the parent?
Hi Tom,
If you see the differece between this code
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
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?
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
Thanks Dennis. I'll try again tomorrow as the email site is down at the moment.
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
From the home page i can get a list of all property listings by doing
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
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
Hi Paul,
Could you try something like this, and see if it works like it should
Hope this helps,
/Dennis
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
is working on a reply...
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.