Copied to clipboard

Flag this post as spam?

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


  • Dan 1288 posts 3921 karma points c-trib
    Nov 12, 2012 @ 16:19
    Dan
    0

    Media sorting in Razor

    Hi,

    I have the following razor snippet which calls a helper method to output all images in a media folder as a series of thumbnails in HTML.  It works fine except the order of the items doesn't seem to be the same as the sort order in the media folder.  If anything it seems that the items are ordered roughly by publication date/time, but even that isn't reliable.

    Here's the code that shows how the items are generated:

    @{
        if (String.IsNullOrEmpty(@Parameter.MediaFolder))
        {
            <div>A media folder has not been selected</div>
        }
        var folderId = @Parameter.mediaFolder;
        var media = Model.MediaById(folderId);
        var items = media.Children;
        if(items.GetType() == typeof(DynamicNull))
        {
            <div>Cannot find the media folder (@folderId)</div>
        }
        else if(items.Count() <= 0)
        {
        <div>Media folder is empty</div>
        }
        else
        {
            @BuildImageList(items);
        }
    }

    Can anyone suggest how to reliably get the media items output in the same order they appear in the media tree?

  • Mike Chambers 636 posts 1253 karma points c-trib
    Nov 12, 2012 @ 16:21
    Mike Chambers
    0

    .OrderBy("SortOrder")?

  • Dan 1288 posts 3921 karma points c-trib
    Nov 12, 2012 @ 16:26
    Dan
    0

    Still can't edit posts on this forum!  Should have said, it's Umbraco 4.9.1.

    Thanks @Mike.  I've tried that - changed:

    var items = media.Children;

    to

    var items = media.Children.OrderBy("SortOrder");

    but it errors on the web page:

    Error loading MacroEngine script (file: gallery.cshtml)

    Should this be sorted somewhere else (in the helper?)  Perhaps it's a silly question, but I've not done much razor at all so just trying to figure out the basics.  Saying that, I'd have expected items to be returned in their sorted order by default, like they are in XSLT, but there may be a reason why not with razor.

  • Mike Chambers 636 posts 1253 karma points c-trib
    Nov 12, 2012 @ 16:30
    Mike Chambers
    0

    if you stick ?umbDebugShowTrace=true on the end of the url and view the stack trace, you'll get the reall error info rather than just the less than useful one you are seeing.

    (this does require {add key="umbracoDebugMode" value="true" /} in the web.config)

     

    actually you might not get a sortorder on media enumerations... :-(

  • Dan 1288 posts 3921 karma points c-trib
    Nov 12, 2012 @ 16:37
    Dan
    0

    Thanks Mike.  Yes, it's not seeming to recognise "SortOrder" as a property.  Sorting by Id works fine, but that's different to what's needed. I'm struggling to think of a nice way around this if there's no sort order property to rely on.

    I'll see if I can have a look in the core to see if I can tell what determines the order of media items returned by 'Model.MediaById(folderId).Children'.

  • Mike Chambers 636 posts 1253 karma points c-trib
    Nov 12, 2012 @ 18:00
    Mike Chambers
    0

    Think media enumerations use the examine index.

  • Funka! 398 posts 661 karma points
    Nov 13, 2012 @ 03:43
    Funka!
    0

    Hey Dan,

    Take a look at U4-873 in the issue tracker and please give it an upvote to increase its visibility. There is also some related discussion in U4-888, where you can find a workaround towards the bottom of the discussion. For what it's worth, here's how I am doing this:

    @using umbraco.MacroEngines
    @using umbraco.cms.businesslogic.media
    @using System.Xml.Linq
    @inherits umbraco.MacroEngines.DynamicNodeContext
    ...
    foreach (Media childMedia in mediaFolder.Children)
    {
        var dynMedia = Library.MediaById(childMedia.Id);  // DynamicMedia
        <li><img src="@dynMedia.umbracoFile" alt="@dynMedia.Name" /></li>
    }

    Not very efficient, and not very obvious, but gets the items in their expected order. Take a look at U4-873 and U4-888 like I mentioned for additional details/comments.

    Good luck!

Please Sign in or register to post replies

Write your reply to:

Draft