Copied to clipboard

Flag this post as spam?

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


  • Kilgore Trout 2 posts 71 karma points
    Dec 07, 2016 @ 18:19
    Kilgore Trout
    0

    Randomly selecting an image from a folder

    I have created a folder in Umbraco and I would like to display a random image from that folder on my page.

    I found a solution from a few years ago which was giving me compile errors

    dynamic folder = Library.MediaById(1054);
    var randomImage = folder.Children.Where("nodeTypeAlias = \"Image\"").Random();
    

    I found that I have to add the proper inherits in my file

     @using umbraco.MacroEngines
     @inherits DynamicNodeContext 
    

    but this it gives me an error because I already have a @model I'm using in the first line

    The 'inherits' keyword is not allowed when a 'model' keyword is used.
    

    Thanks for any help

  • Søren Kottal 530 posts 3521 karma points MVP 2x c-trib
    Dec 21, 2016 @ 21:19
    Søren Kottal
    0

    Can you do the following?

    var folder = Umbraco.TypedMedia(1054);
    var randomImage = folder.Children().Where(x => x.DocumentTypeAlias == "Image").Random();
    
  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Dec 26, 2016 @ 15:22
    Alex Skrypnyk
    0

    Hi Kilgore,

    The problem is with the header of your view.

    You can't use model and inherits directives.

    Also do not use dynamic types, it will be easier to manage this code after.

    Thanks,

    Alex

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies