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>
}
}
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.
@{
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.
@{
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");
}
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'
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".
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:
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.
returns: Umbraco.Core.Models.PublishedContent.PublishedContentEnumerable;
returns: 1291
returns error: 'Umbraco.Core.Models.PublishedContent.PublishedContentEnumerable' does not contain a definition for 'Id'
Hey Jon, try this:
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!
Try also using TypedMedia instead of Media because Media is dynamic (if I remember correctly)
Here's a strongly typed version for prudence.
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.
Glad you figured it out :)
is working on a reply...