Try as I might and looking at outdated documentation! can someone please let me know whats the best way to access media inside a razor view seeing as there's no longer @Library (which is what the documentation tells you to use?) and MediaService is for codebehind?!
Please take a look a at the current documentation page for the Media picker, there you will find code examples for both Mvc and Razor macros.
When looking for current documentation, always click the documentation tab in the main navigation of this site then use the search, that will be filtered to search current "documentation" only and wont show you the old Wiki documentation content.
Thanks very much guys it just seems like that would be more under razor or views in documentation than using umbraco's back-end because that feels more user oriented..
how would one deal with Media Folders and loop over the child contents in v6?
Media In v6 Razor View .cshtml
Hi All,
Try as I might and looking at outdated documentation! can someone please let me know whats the best way to access media inside a razor view seeing as there's no longer @Library (which is what the documentation tells you to use?) and MediaService is for codebehind?!
Thanks,
Tom
Hi,
you can use Umbraco.Media(id of your media(s)) in your Razor File. As an equivalent you could use Umbraco.Content() for Content Nodes.
For both Methods you can put in single or multiple ints or string and it will return Dynamic object with your Content or Media Objects.
Hope this helps
Toby
Hi Tom,
Please take a look a at the current documentation page for the Media picker, there you will find code examples for both Mvc and Razor macros.
When looking for current documentation, always click the documentation tab in the main navigation of this site then use the search, that will be filtered to search current "documentation" only and wont show you the old Wiki documentation content.
Thanks,
Jeavon
Thanks very much guys it just seems like that would be more under razor or views in documentation than using umbraco's back-end because that feels more user oriented..
how would one deal with Media Folders and loop over the child contents in v6?
Thanks so much
This should do the Trick:
//get Media Folder
var mediaFolder = Umbraco.Media(Model.Content.GetPropertyValue("mediaPickerProperty"));
//iterate over Children
foreach (var mediaItem in mediaFolder.Children)
{
if (mediaItem.DocumentTypeAlias == "Image")
{
<img src="@mediaItem.umbracoFile" width="@mediaItem.umbracoWidth" height="@mediaItem.umbracoHeight" alt="@mediaItem.Name"/>
}
else
{
//handle other types
}
}
Thanks Tobias I ended up with a global helper like so:
public static IEnumerable<Umbraco.Core.Models.IPublishedContent> GetImagesForMediaFolder(Umbraco.Web.UmbracoHelper umbracoHelper, int mediaFolderId)
{
List<IPublishedContent> result = new List<IPublishedContent>();
IPublishedContent mediaFolder = umbracoHelper.TypedMedia(mediaFolderId);
if (mediaFolder != null && mediaFolder.Children.Any())
{
result.AddRange(mediaFolder.Children.Where(i => i.DocumentTypeAlias == "Image"));
}
return result;
}
is working on a reply...