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 702 posts 4497 karma points MVP 5x 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 6133 posts 23952 karma points MVP 7x 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

Please Sign in or register to post replies

Write your reply to:

Draft