Copied to clipboard

Flag this post as spam?

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


  • Jon Free 14 posts 34 karma points
    May 15, 2012 @ 17:00
    Jon Free
    0

    Testing image crops existence with Razor in Umbraco

    I have created some Razor code to output images onto a page if the exist. It is detailed below and contains some simple checks to prevent rendering a blank list item. The site has gone live and works fine. The client then deleted the image from the media folder within Umbraco, meaning my node had a valid image assigned but the image just didn't exists. I got the following exception:

    'string' does not contain a definition for 'crops'

    How do I deal with this?

    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.presentation.nodeFactory
    @using umbraco.cms.businesslogic.media
    
    <ul>
        @foreach (dynamic client in @Model.Children)
        {
            var image = Model.MediaById(client.Logo);
            var crops = image.imageCropper.crops;
    
            <li>
                <h2><span>@client.Name</span></h2>
    
                @if (crops != null || crops.GetType().ToString() != "System.String")
                {
                    <span class="itemImage">
                        <img src="@crops.Find("@name", "cropname").url" alt="@client.Name" />
                    </span>
                }
            </li>
        }
    </ul>

  • Dave Woestenborghs 3216 posts 10808 karma points MVP 4x admin c-trib
    May 16, 2012 @ 10:24
  • Jon Free 14 posts 34 karma points
    May 16, 2012 @ 10:46
    Jon Free
    0

    Dave, 

    Thanks for the feedback Dave but that was my post! I was struggling with actually using the image cropper in Razor but got it working in the end. Your comment about the helper method only seemed relevant for previous versions of Umbraco Razor where dynamic nodes were not returned. Is that right or would your technique still work?

  • Dave Woestenborghs 3216 posts 10808 karma points MVP 4x admin c-trib
    May 16, 2012 @ 10:55
    Dave Woestenborghs
    0

    Hi Jon,

     

    I'm using this code in a 4.7.1 version of Umbraco and it works there.

     

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft