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"/>
}
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.
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"/>
}
}
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:
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?
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
Yours Dirk
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:
...
:)
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.
I might be wrong, but in your
media
variable, it looks like you’re returning multiple (referring to theIEnumerable<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.
Thanks Rhys,
Your solution worked!
I see where my mistake was now, referring to the IEnumerable
Perfect, glad that resolved things! :)
is working on a reply...