Copied to clipboard

Flag this post as spam?

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


  • Morten 105 posts 345 karma points
    Oct 26, 2017 @ 11:38
    Morten
    1

    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.

    Thanks

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Oct 26, 2017 @ 12:09
    Michaël Vanbrabandt
    0

    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

  • Morten 105 posts 345 karma points
    Oct 26, 2017 @ 12:13
    Morten
    0

    Pretty simple:

    Content

    Products
        Product 1 (Product 1 doctype)
        Product 2 (Product 1 doctype)
        Product 3 (Product 2 doctype)
        Product .......
    

    Document Types

    Products (folder)
        Products
        Product 1
        Product 2
        ProductOverview
    

    Product 1 and Product 2 are the two different doctypes (with different templates), but they both have ProductOverview as a composition.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Oct 26, 2017 @ 12:21
    Michaël Vanbrabandt
    0

    Ok now its clear.

    Can you try to access the OverviewImage property by using GetPropertyValue("alias") ?

    @foreach(var product in Model.Content.Children) {
        <img src="product.GetPropertyValue("overviewImage").First().Url" />
    }
    

    /Michaël

  • Morten 105 posts 345 karma points
    Oct 26, 2017 @ 12:23
    Morten
    0

    'object' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of type 'object' could be found

    That's what I'm getting.

  • Morten 105 posts 345 karma points
    Oct 26, 2017 @ 12:23
    Morten
    0

    'object' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of type 'object' could be found

    That's what I'm getting.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Oct 26, 2017 @ 12:25
    Michaël Vanbrabandt
    0

    What kind of property is overviewImage ?

  • Morten 105 posts 345 karma points
    Oct 26, 2017 @ 12:25
    Morten
    0

    Media Picker

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Oct 26, 2017 @ 12:31
    Michaël Vanbrabandt
    0

    Can you do the following:

    @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" />
    
    }  
    
  • Morten 105 posts 345 karma points
    Oct 26, 2017 @ 12:38
    Morten
    0

    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.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Oct 26, 2017 @ 12:44
    Michaël Vanbrabandt
    0

    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):

    @foreach((dynamic) product in Model.Content.Children) {
    
        <img src="product.OverviewImage.First().Url" />
    
    } 
    

    Was just going to test this out, but maybe it will be faster if you tried this?

    /Michaël

  • Morten 105 posts 345 karma points
    Oct 26, 2017 @ 12:49
    Morten
    0

    ''System.Collections.Generic.List

    Nor does it have a definition for Url. This is odd, because I'm sure List has a definition for First().

  • Morten 105 posts 345 karma points
    Oct 26, 2017 @ 12:57
    Morten
    101

    Okay. I was able to resolve this by doing:

    @foreach(var product in Model.Content.Children) {
        dynamic image = product;
        <img src="@image.OverviewImage[0].Url" />
    }
    

    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!

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Oct 26, 2017 @ 12:58
    Michaël Vanbrabandt
    0

    Hi Morten,

    glad that you have solved it!

    Have a nice day

    /Michaël

  • Morten 105 posts 345 karma points
    Oct 26, 2017 @ 13:01
    Morten
    0

    Thank you, you too!

Please Sign in or register to post replies

Write your reply to:

Draft