Testing the existence of image cultures with Razor at Umbraco

I created Razor code to output images to a page if they exist. It is described in detail below and contains some simple checks to prevent the display of an empty list item. The site began to live and works fine. Then the client deleted the image from the media folder in Umbraco, that is, my node had a valid image assigned, but the image simply did not exist. I got the following exception:

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

How can I handle 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>
+3
source share
2 answers

, , . , MediaById DynamicNode, , :

if(image.GetType() == typeof(DynamicNode))
{
    ...
}
0

. , Model.MediaById(imageid) , ( ).

, :

dynamic mainMediaImage = new DynamicNull();
try
{
  mainMediaImage = Model.MediaById(related.eventMainImage);
}
catch(Exception e)
{
  <p style='display: none;'>@e.Message</p>
}
var cropUrl = "";

if(mainMediaImage.GetType() == typeof(DynamicMedia))
{  
   cropUrl = GetImageCropperUrl(Model.MediaById(related.eventMainImage).crops, "List Image");    
}       

, .

, DynamicMedia, ... catch() , .

0

All Articles