Copied to clipboard

Flag this post as spam?

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


  • Ambert van Unen 175 posts 819 karma points c-trib
    Sep 24, 2019 @ 20:51
    Ambert van Unen
    0

    Cant get children of Mediafolder

    Hi,

    I've just updated to 8.1.5 and am having the following issue.

    Old code in a controller:

                var imagePicker = campingPage.Value<IEnumerable<IPublishedContent>>("images");
                var imageUrl = "/placeholder.jpg";
                //Check if folder is selected
                if (imagePicker.Any())
                {
                    //Select the folder
                    var imageNode = imagePicker.First();
    
                    //Check if the folder has any children(images)
                    if (imageNode.Children.Any())
                    {
                        //Get the first image
                        var image = imageNode.Children().First();
    

    Only this has stopped working. I've been trying for a few hours now and am quite stuck. The mediafolder always has 'null' children.

    var imagePicker = campingPage.Value<IPublishedContent>("images");
    var imagePickertest = campingPage.Images;
    var mediaId = imagePicker.Id;
    var mediaFolder = Umbraco.Content(mediaId);
    

    All of these(except the MediaId) result in a null value when adding .Children().. However, it ofcourse DOES have children.

    Fun fact when having similar code on a normal TemplatePage, it DOES work

    @helper RenderItem(CampingPage camping)
    {
    var imagePicker = camping.Images;
    var imageUrl = "/placeholder.jpg";
    if (imagePicker != null)
    {
        if (imagePicker.Children().Any())
        {
            var image = imagePicker.Children().First();
    

    Hope anyone has an idea, bit stuck right now x)

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Sep 24, 2019 @ 21:37
    Marc Goodson
    0

    Hi Ambert

    Is your Media Picker configured to pick multiple items? or a single folder?

    I wonder if in the controller the PropertyValueConverter is being clever and detecting that only a single media item is pickable and therefore the item is convertable to an IPublishedContent item rather than an IEnumerable

    eg try:

    var imagePicker = campingPage.Value<IPublishedContent>("images");
    

    My guess is based on this line in the source code:

    https://github.com/umbraco/Umbraco-CMS/blob/3bfd9b71e290354744d677440983ecd06c5c0788/src/Umbraco.Web/PropertyEditors/ValueConverters/MediaPickerValueConverter.cs#L37

    I'm wondering if in the View that ModelsBuilder isn't being quite so clever and the generated Images property is an IEnumerable<IPublishedContent> and that's why it works...

    regards

    Marc

  • Ambert van Unen 175 posts 819 karma points c-trib
    Sep 25, 2019 @ 19:06
    Ambert van Unen
    100

    Ok I've got it working, but I'm not sure why. It's definitly related to the changes in .Children and .Children() (with the culture)

    In my Controller I've changed the code to use .Children, and it works... Whilst in my Templatepage I'm using .Children()

    GG ;-)

    Final code:

                var imagePicker = campingPage.Images;
    
                var imageUrl = "/placeholder.jpg";
                if (imagePicker != null)
                {
                    if (imagePicker.Children.Any())
                    {
                        var image = imagePicker.Children.First();
    
  • Ambert van Unen 175 posts 819 karma points c-trib
    Oct 04, 2019 @ 12:03
    Ambert van Unen
    0

    Seems to be some kind of cache issue why its still working in the templates.. After checking a few days later, that stopped working too... GG ;-)

Please Sign in or register to post replies

Write your reply to:

Draft