Copied to clipboard

Flag this post as spam?

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


  • MB 273 posts 936 karma points
    Jul 20, 2016 @ 16:06
    MB
    0

    Accessing media folder to fetch image

    Hallo everyone,

    I'm following this guide: Umbraco 7 and MVC – Banners which works "okay", the only issue is that I can't fetch any data from the media file.

    My code looks like this: https://jsfiddle.net/p8d490wy/

    I can get the mediaFolderId ID but I think it's the banners variable?

    Under my "media" I have a folder entitled "Home Page Banners" which has the "banner media-type" inside with a text.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 20, 2016 @ 17:01
    Alex Skrypnyk
    1

    Hi Mike,

    For each banner from Banners folder you have to get url of image.

    @foreach (var banner in banners)
            {
                <div>
                    @(banner.Url)
                </div>
            }
    

    What is bannerText property?

    I hope I will help you.

    Thanks,

    Alex

  • MB 273 posts 936 karma points
    Jul 20, 2016 @ 17:53
    MB
    0

    Hey Alex,

    Thank you for your reply :o)

    I can't seem to get ANYTHING within the foreach. I can grap the ID from mediaFolderID.

    The description/text for the banner is just a simple textstring with the alias text

    If I write @(banner.Url) it also returns nothing. I can't get any code from within the @foreach to be displayed and I honestly can't figure out why.

    Here's my media type: enter image description here

    Here's my Media: enter image description here

    Here's my frontpage: enter image description here

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 20, 2016 @ 20:50
    Alex Skrypnyk
    1

    Hi Mike,

    I don't see from screens where do you store images ?

    I see you are using Banner Media type, but is there image uploaded?

    Thanks,

    Alex

  • MB 273 posts 936 karma points
    Jul 21, 2016 @ 07:11
    MB
    0

    Hey Alex,

    At this time I don't store images anywhere. I removed everything and simply added a textstring to see if I could get it's data :o)

    But I just created an Upload property. The image is stored in Media -> Home Page Banners along with the text property as you can see in image 2. The uploaded image is of media-type: banner

    But I also tried just writing a simple "Hello world" textstring inside the @foreach which doesn't work either.

     Response.Write(mediaFolder);
     Returns: `Umbraco.Web.PublishedContentModels.Banner`
    
    Response.Write(banners);
    System.Collections.Generic.List`1[Umbraco.Core.Models.IPublishedContent]
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 21, 2016 @ 21:11
    Alex Skrypnyk
    1

    Hi Mike,

    Can you please provide code of all view?

    Hard to understand what is the problem.

    Thanks

  • MB 273 posts 936 karma points
    Jul 22, 2016 @ 20:18
    MB
    0

    Hey Alex,

    Well the only code is the jsfiddle I posted in the first post https://jsfiddle.net/p8d490wy/

    I'm just following the tutorial/guide :) Bascially what I do is:

    1. Create a Media-Type entitled banner
    2. Inside the Media-type banner I made a textstring
    3. In my document type Frontpage I made a tab called Banners with a media picker
    4. I go to Content and open up my frontpage and inside my Banners tap I select the file inside media as you can see from the above pictures

    The problem is that the @foreach from the jsfiddle code doesn't work. I can't get anything from within the @foreach to display anything.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 23, 2016 @ 09:55
    Alex Skrypnyk
    100

    Hi Mike,

    But if you iterate over the child items - you have to select folder, not item inside folder.

    You can get your folder like:

    var mediaFolder = mediaItem.Parent;
    
    var banners = mediaFolder.Children(x => x.DocumentTypeAlias == "banner").ToList(); 
    

    Thanks,

    Alex

  • Ian 178 posts 752 karma points
    Jul 23, 2016 @ 10:18
    Ian
    1

    To help further you will need to break things down (which you have already started to do).

    1. put a breakpoint inside the foreach loop on the 'GetPropertyValue' line.

    2. Does your code land on that line at all and pause execution?

    3. What is the value of banner at this point does it look like the media item you expect.

    4. Look for a 'properties' object (just as a debugging exercise). It might be in 'non public properties' in the debugger.

    5. Is your text property in there.

    Hopefully at this point something obvious will reveal itself like a typo on your doctype or property.

    If not you may have a problem with your content cache try re saving your media items and select republish entire site on your content tree.

  • MB 273 posts 936 karma points
    Aug 01, 2016 @ 09:16
    MB
    1

    First of all, huge thanks for all the help! As Alex hinted out, I don't have to select the item INSIDE the folder. I need to select THE FOLDER (not the item inside).

    So this code:

    var mediaFolderId = (int)CurrentPage.BannersMediaFolder;
    var mediaFolder = Umbraco.TypedMedia(mediaFolderId);
    

    It selects the item inside my folder. I looked at Alex's idéa and added .Parent to the mediaFolderID like so:

    var mediaFolder = Umbraco.TypedMedia(mediaFolderId).Parent;
    

    Which works! I how select the FOLDER instead of the item inside and that works :)

    insanely high high-five to ya'll ;D

Please Sign in or register to post replies

Write your reply to:

Draft