Copied to clipboard

Flag this post as spam?

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


  • Jon 47 posts 290 karma points
    Aug 22, 2018 @ 19:34
    Jon
    0

    folder id only works hardcoded...

    Im trying to load images from a folder ind the media library. It works fine with hardcoded id (1291) but if I change it to CurrentPage.billeder I get error:

    System.Array' does not contain a definition for 'Children'

    @CurrentPage.billeder
    
    @{
        var media = Umbraco.Media(1291); 
        var selection = media.Children("Image");
    
        if (selection.Any())
        {
            <ul>
                @foreach (var item in selection)
                {
                    <li>
                        <img src="@item.GetCropUrl("large")" alt="@item.Name" />
                    </li>
                }
            </ul>
        }
    }
    
  • Rami H 13 posts 141 karma points
    Aug 22, 2018 @ 21:42
    Rami H
    0

    Hi Jon,

    Sorry I had further edits after thinking about your problem here.

    If you can check the output of @CurrentPage.billeder.GetType() and confirm that you get an integer? If it shows your type to be of DynamicPublishedContent, then you'll want to pass in "CurrentPage.billeder.Id".

    //check type of returned data
    @CurrentPage.billeder.GetType()
    
    //if type is of DynamicPublishedContent, then use Id.
    var media = Umbraco.Media(CurrentPage.billeder.Id);
    

    If that doesn't work, and you're getting back a type of string or integer, it could be in how you're assigning the variable "selection". Using dynamics you'll want to call your children of a certain type by using pluralized collections Check out dynamic collections

    So you should try something like the following instead:

    var selection = media.Images;
    

    Just as a reminder, you should really look into using strongly typed methods instead of dynamics as dynamics will soon be gone and phased out from my understanding. Let us know how it goes.

  • Jon 47 posts 290 karma points
    Aug 23, 2018 @ 05:36
    Jon
    0
    @CurrentPage.billeder.GetType();
    

    returns: Umbraco.Core.Models.PublishedContent.PublishedContentEnumerable;

    @CurrentPage.billeder
    

    returns: 1291

    @CurrentPage.billeder.Id;
    

    returns error: 'Umbraco.Core.Models.PublishedContent.PublishedContentEnumerable' does not contain a definition for 'Id'

  • Rami H 13 posts 141 karma points
    Aug 23, 2018 @ 13:59
    Rami H
    100

    Hey Jon, try this:

    @{
        var media = Umbraco.Media(CurrentPage.billeder.ToString()); 
        var selection = media.Children("Image");
     }
    

    Looking at the types is important because while outputting in your razor view, the engine will try to determine the best thing to output, which is why seeing "1291" being output could be misleading, as the type is not an integer or string. I think .Media() can accept either an int or a string, but not PublishedContentEnumerable.

    Hopefully that solves it, let us know!

  • Harry Spyrou 212 posts 604 karma points
    Aug 23, 2018 @ 14:31
    Harry Spyrou
    1

    Try also using TypedMedia instead of Media because Media is dynamic (if I remember correctly)

  • Rami H 13 posts 141 karma points
    Aug 29, 2018 @ 20:44
    Rami H
    0

    Here's a strongly typed version for prudence.

    @{
        var media = Umbraco.TypedMedia(Model.Content.GetPropertyValue<int>("billeder"));
        var selection = media.Children(x => x.DocumentTypeAlias == "Image");
    }
    

    If you're using a late enough version of Umbraco and have property converters enabled, you could do all of this in one line. You can request an IPublishedContent object directly and bypass the Umbraco.[Typed]Media() method altogether.

    @{
       var selection = Model.Content.GetPropertyValue<IPublishedContent>("billeder").Children(x => x.DocumentTypeAlias == "Image");
    }
    

    Glad you figured it out :)

Please Sign in or register to post replies

Write your reply to:

Draft