Copied to clipboard

Flag this post as spam?

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


  • Tom 713 posts 954 karma points
    Jul 02, 2013 @ 08:38
    Tom
    0

    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

  • Tobias Neugebauer 52 posts 93 karma points
    Jul 02, 2013 @ 09:59
    Tobias Neugebauer
    0

    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

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jul 02, 2013 @ 12:00
    Jeavon Leopold
    100

    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

  • Tom 713 posts 954 karma points
    Jul 03, 2013 @ 06:14
    Tom
    0

    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

  • Tobias Neugebauer 52 posts 93 karma points
    Jul 03, 2013 @ 07:39
    Tobias Neugebauer
    1

    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
        }
    }

  • Tom 713 posts 954 karma points
    Jul 03, 2013 @ 07:43
    Tom
    0

    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;

        }

     

Please Sign in or register to post replies

Write your reply to:

Draft