How to access composite values on different doctypes? Getting an "Unable to cast" error
So I have a Document Type which has a lot of child pages. These child pages offer different layouts, so I made a doctype for each of them. Now I've created a bunch of content with these different doctypes, but there's a problem: I want to access the properties of the child elements' composition doctype, which has properties shared on all of them.
Imagine this: You have a products page, which has a lot of products in them. You need to have two doctypes: Products and Product. I have 3x Product with some different properties, but I also have a Product overview (alias is ProductOverview) doctype, which contains the propeties for each Product, but which is only shown on the Products page.
I am trying to do this, but I cannot cast it and it doesn't work like that:
Products.cshtml
@foreach(ProductOverview product in Model.Content.Children) {
<img src="product.OverviewImage.First().Url" />
}
But I cannot cast ProductOverview to Product1 (doctype alias is Product1).
If I do something like change the @foreach(ProductOverview product in Model.Content.Children) to @foreach(Product1 product in Model.Content.Children) then it also gives me a cast error. I know all the children have these propeties, so I don't need to check that.
@foreach(var product in Model.Content.Children) {
// Get the id's of the selected images from your media picker
var overviewList = product.GetPropertyValue<string>("overviewImage").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
// Get the Umbraco media file
var overviewImage = Umbraco.TypedMedia(overviewList.First());
<img src="overviewImage.Url" />
}
System.FormatException: 'Input string was not in a correct format.'
I guess this is a workaround, but is this really the best way to do it? I'm using dynamic models, so I should be able to, somehow, grab the value normally.
How to access composite values on different doctypes? Getting an "Unable to cast" error
So I have a Document Type which has a lot of child pages. These child pages offer different layouts, so I made a doctype for each of them. Now I've created a bunch of content with these different doctypes, but there's a problem: I want to access the properties of the child elements' composition doctype, which has properties shared on all of them.
Imagine this: You have a products page, which has a lot of products in them. You need to have two doctypes:
Products
andProduct
. I have 3xProduct
with some different properties, but I also have aProduct overview
(alias isProductOverview
) doctype, which contains the propeties for eachProduct
, but which is only shown on theProducts
page.I am trying to do this, but I cannot cast it and it doesn't work like that:
Products.cshtml
But I cannot cast
ProductOverview
toProduct1
(doctype alias isProduct1
).If I do something like change the
@foreach(ProductOverview product in Model.Content.Children)
to@foreach(Product1 product in Model.Content.Children)
then it also gives me a cast error. I know all the children have these propeties, so I don't need to check that.Thanks
Hi Morten,
How did you set up the node structure and the permissions for all these document types, can you show us this?
/Michaël
Pretty simple:
Content
Document Types
Product 1
andProduct 2
are the two different doctypes (with different templates), but they both haveProductOverview
as a composition.Ok now its clear.
Can you try to access the
OverviewImage
property by usingGetPropertyValue("alias")
?/Michaël
That's what I'm getting.
That's what I'm getting.
What kind of property is
overviewImage
?Media Picker
Can you do the following:
I guess this is a workaround, but is this really the best way to do it? I'm using dynamic models, so I should be able to, somehow, grab the value normally.
Morten,
correct, was thinking of a workaround because you have different child doc types. Another solution that I was thinking of was casting to
(dynamic)
:Was just going to test this out, but maybe it will be faster if you tried this?
/Michaël
Nor does it have a definition for
Url
. This is odd, because I'm sureList
has a definition forFirst()
.Okay. I was able to resolve this by doing:
and that also means my Vorto values work simply by doing
product.GetVortoValue("overviewDescription")
.Excellent. So it's a workaround, but it works I guess. Thanks!
Hi Morten,
glad that you have solved it!
Have a nice day
/Michaël
Thank you, you too!
is working on a reply...