Annoying problem. Using .Parent.Parent is there a better way?
So I have a bit of an annoying issue.
Umbraco 7, Razor and trying to traverse but using 'AncestorsOrSelf' is giving me an IPublishedContent error about it not having "HasValue", and 'AncestorOrSelf' brings back a "False", so I am using .Parent.Parent as an option to do 2 level traversing. Even though I have tried "AncestorsOrSelf(2)" and "AncestorOrSelf(2)". There has to be a better way. Any ideas?
More or less, I am trying to get the logo of a parent node that is a 'Subsite' of my main home node.
Are you a little confused here...? I'm assuming the code you've posted is on the Subsite Page.
Where are you trying to get the subsite Logo from - from the Home node at the top or from the subsite Home?
For Home it would be Model.Content.AncestorOrSelf(1) for the SubsiteHome it would be Model.Content.AncestorOrSelf(2).
The reason I ask is from the name of the property subsiteLogo I would expect it to be on the SubsiteHome but Parent.Parent will be serving the Home node here... so you'd expect just a single Parent..
If you run something like:
@{
var home = Model.Content.AncestorOrSelf<Home>(1);
<h2>home = @home.Id @home.Name </h2>
<h2>Same as @Model.Content.Parent.Parent.Id @Model.Content.Parent.Parent.Name</h2>
<img src="@(home.GetPropertyValue<IPublishedContent>("subsiteLogo").Url)" />
}
@{
var subsiteHome = Model.Content.AncestorOrSelf<SubsiteHome>(2);
<h2>subsiteHome = @subsiteHome.Id @subsiteHome.Name </h2>
<h2>Same as @Model.Content.Parent.Id @Model.Content.Parent.Name</h2>
<img src="@(subsiteHome.GetPropertyValue<IPublishedContent>("subsiteLogo").Url)" />
}
You should get the home and then the subsite home with the same node Ids and names...
The only other thing I can think of is you've bound a host name to the subsite??
Annoying problem. Using .Parent.Parent is there a better way?
So I have a bit of an annoying issue.
Umbraco 7, Razor and trying to traverse but using 'AncestorsOrSelf' is giving me an IPublishedContent error about it not having "HasValue", and 'AncestorOrSelf' brings back a "False", so I am using .Parent.Parent as an option to do 2 level traversing. Even though I have tried "AncestorsOrSelf(2)" and "AncestorOrSelf(2)". There has to be a better way. Any ideas?
More or less, I am trying to get the logo of a parent node that is a 'Subsite' of my main home node.
Tree lays out like this
Home
//=== code is below
Hi Carlos,
Are you a little confused here...? I'm assuming the code you've posted is on the Subsite Page.
Where are you trying to get the subsite Logo from - from the Home node at the top or from the subsite Home?
For Home it would be Model.Content.AncestorOrSelf(1) for the SubsiteHome it would be Model.Content.AncestorOrSelf(2).
The reason I ask is from the name of the property subsiteLogo I would expect it to be on the SubsiteHome but Parent.Parent will be serving the Home node here... so you'd expect just a single Parent..
If you run something like:
You should get the home and then the subsite home with the same node Ids and names...
The only other thing I can think of is you've bound a host name to the subsite??
is working on a reply...