(Razor) Looping through images in a Media folder - can't reference umbracoFile
Hi y'all,
Ok, this is steadily driving me insane and I've no idea why this isn't working. I've written some Razor which happily checks a media picker (alias "kerbImagesGallery") to see if any images are stored within the folder and if so loops through them.
@{ dynamic mediaFolderId = Model.kerbsImageGallery; DynamicMedia folder = new DynamicMedia(mediaFolderId); if (folder != null && folder.Children.Items.Count() > 0) { foreach (dynamic media in folder.Children.Items) { <p><a href="@media.umbracoFile" class="fancybox" rel="kerbs" title="@media.altTag"> <img src="@media.Name" alt="@media.altTag" title="@media.altTag"> </a></p> } } }
I can happily reference each item name (@media.Name), and also a Alt Text custom property I added to the media datatype (@media.altTag) BUT for some reason I cannot reference the image path (@media.umbracoFile) or indeed any of the other values such as width, height, size etc.
If I reference @media.Id I get the ID value of each image item within that folder so why can't I get the path?
Maybe the conversion from DynamicMedia to dynamic isn't quite right, have you tried using var folder = Library.MediaById(mediaFolderId) instead as that will return a dynamic object?
I reworked a few things and pretty much got the exact same code, I added a few bits in to just test the output which all work fine. Everything work but still @media.umbracoFile returns nothing.
@{ var mediaFolderId = Model.kerbsImageGallery; var folder = Library.MediaById(mediaFolderId);
Just for the sake of trying something new I just removed one of the files from the folder and reuploaded it to Media - now it is working!!!! *facepalm*
Just can't think why this would happen - bad initial upload maybe? Anyway the code you supplied was good - thanks Jeavon.
Could I be cheaky and ask one further question. Do you know how I'd go about adding a class to all but the very first image it loops through? So as an example when rendered it would look like:
Works like a charm! Thanks for all your help sorting this one Jeavon. I've marked your previous reply as the solution as that was the topic at hand, but very much appreciate the extra item here. Thanks again! :)
(Razor) Looping through images in a Media folder - can't reference umbracoFile
Hi y'all,
Ok, this is steadily driving me insane and I've no idea why this isn't working. I've written some Razor which happily checks a media picker (alias "kerbImagesGallery") to see if any images are stored within the folder and if so loops through them.
I can happily reference each item name (@media.Name), and also a Alt Text custom property I added to the media datatype (@media.altTag) BUT for some reason I cannot reference the image path (@media.umbracoFile) or indeed any of the other values such as width, height, size etc.
If I reference @media.Id I get the ID value of each image item within that folder so why can't I get the path?
Thanks vm
Maybe the conversion from DynamicMedia to dynamic isn't quite right, have you tried using
var folder = Library.MediaById(mediaFolderId)
instead as that will return a dynamic object?Hi Jeavon, I did try this. I'm getting a conflict on the count reference though:
I've removed the count() reference by way of a quick test and no errors but the @media.umbracoFile value is still returned as empty.
Hi Mike,
I just gave this snippet a go on v6.1.6 and it worked fine:
Maybe try that, if it doesn't work, what version of Umbraco are you using?
Jeavon
Hi there Jeavon,
I reworked a few things and pretty much got the exact same code, I added a few bits in to just test the output which all work fine. Everything work but still @media.umbracoFile returns nothing.
I am also using v6.1.6
Just tried cutting/pasting your code in case I had missed something really silly, like a typo, but nope! :( Just makes no sense.
Just for the sake of trying something new I just removed one of the files from the folder and reuploaded it to Media - now it is working!!!! *facepalm*
Just can't think why this would happen - bad initial upload maybe? Anyway the code you supplied was good - thanks Jeavon.
Could I be cheaky and ask one further question. Do you know how I'd go about adding a class to all but the very first image it loops through? So as an example when rendered it would look like:
:-)
Maybe, or could possibly be a corrupted Examine Index, at least it's working now!
Anyway to answer your question:
Works like a charm! Thanks for all your help sorting this one Jeavon. I've marked your previous reply as the solution as that was the topic at hand, but very much appreciate the extra item here. Thanks again! :)
You're very welcome! :)
is working on a reply...