Mapping images using Glassmapper in Sitecore

      No Comments on Mapping images using Glassmapper in Sitecore

I am trying to render images in the foreach loop inside my view. But it is displaying the same image even if Model has got different images. My code in the view:

@foreach (var currentFeature in Model.FacilityFeatures)
        { 
            <li class="features-list-enclosure">
                <span title="@currentFeature.CategoryName" class="features-list-enclosure__item">
                    @RenderImage(x => currentFeature.CategoryICON, new { title = @currentFeature.CategoryName }, isEditable: true)
                </span>
            </li>
        }

My Model:

public class FacilitiesPage : BasePage
    {
        [SitecoreField(FieldName = "Facility features")]
        public virtual IEnumerable<ContentCategory> FacilityFeatures { get; set; }

    }

When I am running the debugger it is showing that currentFeature.CategoryICON item has got different image in each iteration of the loop. But it is showing the same image on the UI but the title on the image is different. If I don’t use the Glassmapper render image than it works and shows different images:

@foreach (var currentFeature in Model.FacilityFeatures)
        { 
            <li class="features-list-enclosure">
                <span title="@currentFeature.CategoryName" class="features-list-enclosure__item">
                    <img title="@currentFeature.CategoryName" src="@currentFeature.CategoryICON.Src" />

                </span>
            </li>
        }

Solution:

This happens because Glass caches the output when the expression is the same. Try using this to render the image:

@RenderImage(currentFeature, x => x.CategoryICON, new { title = @currentFeature.CategoryName }, isEditable: true)

This issue related to this: https://github.com/mikeedwards83/Glass.Mapper/issues/95

 

*Copied from https://stackoverflow.com/questions/32493191/mapping-images-using-glassmapper-in-sitecore

Leave a Reply

Your email address will not be published. Required fields are marked *