Copied to clipboard

Flag this post as spam?

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


  • Shelly Coops 7 posts 88 karma points
    May 30, 2019 @ 10:54
    Shelly Coops
    0

    Unable to loop inside media Folder in Umbraco 8

    I have 2 images in a media folder and I need to display them. I've been trying to loop inside the folder but have not been unable to.

    That's my code:

    var media = Model.Value<IEnumerable<IPublishedContent>>("mediaFolder");
    
    foreach (var item in media){
    <img src="@item.Url"/>
    }
    

    The result is a compilation error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    What I'm I doing wrong here?

  • Dirk Seefeld 126 posts 665 karma points
    May 30, 2019 @ 15:56
    Dirk Seefeld
    0

    Hi Shelly,

    Umbraco 8 has strong typed models of document types. Which give you Intellisense in Visual Studio. This is done by the ModelsBuilder. Please refer to the https://our.umbraco.com/documentation/Reference/Templating/Modelsbuilder/Builder-Modes for details. Using Dll mode gives you the mentioned Intellisense.

    There is another useful object which give access to the data. On a template it is Umbraco (aka UmbracoHelper). This has the method MediaAtRoot() returning an IEnumerable

     var media = Umbraco.MediaAtRoot();
    
    foreach (var item in media){
    <img src="@item.Url"/>
    }
    

    Yours Dirk

  • Dirk Seefeld 126 posts 665 karma points
    May 30, 2019 @ 16:22
    Dirk Seefeld
    0

    I wound what you want to do? Thus I like to show you another approach:

    Add a Media Picker property called "MediaFolder" to your document type. Then you can show all the images (and only images) by the following code:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.[YourDocType]>
    

    ...

       @{
       var mediaFolder = Model.MediaFolder;
       var media = mediaFolder.Children;
       foreach (var item in media.Where(i => i.ContentType.Alias.Equals("Image")))
       {
          <img src="@item.Url" />
       }}
    

    :)

  • Shelly Coops 7 posts 88 karma points
    May 31, 2019 @ 05:08
    Shelly Coops
    0

    Hi Dirk,

    Thanks for the help. I have tried both of your solutions but they still do not work.

    I have uploaded images in my media section and categorized them using folders. The images will be used in a slider and I wanted to code it in a way where I would just be adding images to the media folder and they would dynamically appear in the slider instead of going in the content node and adding the image itself each time using the media picker.

    I am only picking the folder using the media picker in the content node.

    Do you see what I'm trying to do?

    I used to do that in Umbraco 7 but does not work the same way with 8.

  • Rhys Hamilton 140 posts 942 karma points
    May 31, 2019 @ 10:34
    Rhys Hamilton
    101

    I might be wrong, but in your media variable, it looks like you’re returning multiple (referring to the IEnumerable<IPublishedContent>) media folders, when in actual fact, I think you’re only looking to return one?

    Assuming you're media picker is set up to only accept a single item, the following code should hopefully work.

    // Returns a single media folder (not multiple)
    var media = Model.Value<IPublishedContent>("mediaFolder"); 
    
    if (media != null)
    {
        // Get all of the items within the media folder
        foreach (var item in media.Children)
        {
            <img src="@item.Url"/>
        }        
    }
    
  • Shelly Coops 7 posts 88 karma points
    May 31, 2019 @ 12:06
    Shelly Coops
    1

    Thanks Rhys,

    Your solution worked!

    I see where my mistake was now, referring to the IEnumerable

  • Rhys Hamilton 140 posts 942 karma points
    May 31, 2019 @ 12:10
    Rhys Hamilton
    0

    Perfect, glad that resolved things! :)

Please Sign in or register to post replies

Write your reply to:

Draft