5

Environment: Sitecore 9.0.2 + Glassmapper

I am trying to create a component like this. enter image description here

So in the template, for the images (stored in Sitecore Media Library), I have taken a field of type TreeListEx.

From a TreeListEx type field, Sitecore returns the item-ids of the selected item.

So, when I use the syntax @Html.Glass().RenderImage(item, x => x, null, true), the parameter item is the Sitecore Id of the image item and the output of the above syntax is an empty string.

  1. Am I missing something?
  2. Should I make a call to Sitecore for each media item Id to get the item and pass it to the Glass Mapper?

I did saw the link https://horizontalintegration.blog/2018/02/07/sitecore-glass-mapper-gettargetobjectoflamba-exception/ and believe Ram is also passing the Id.

3
  • 2
    Try solution in this article suhasshiv.com/2017/02/18/TDS-Glassmapper-CustomTypes, you can configure glass mapper to use a type instead of guids for list fields. use Glass.Mapper.Sc.Fields.Image as your item type. Oct 5, 2019 at 11:43
  • @VipinBanka: Thanks for the link. I went through the first approach and it worked. Oct 6, 2019 at 9:07
  • @VipinBanka: Please provide it in as answer, so I can mark the same. Oct 6, 2019 at 14:38

2 Answers 2

3

Update your model to have the type of your TreelistEx field be an IEnumerable<Glass.Mapper.Sc.Fields.Image> instead of using IDs. Something like:

using System.Collections.Generic;
using Glass.Mapper.Sc.Fields;

[SitecoreType(TemplateId = "{TEMPLATEID}")]
public class ImageGroup
{
    [SitecoreField(FieldId = "{FIELDID}")]
    public virtual IEnumerable<Image> SelectedImages { get; set; }
}

Note

If you're doing this with code generation with TDS and Glass Mapper, you can use the CustomData property of your field item in TDS to have code generation set the model property type automatically.

Code generation properties in TDS

2

You can create a new child item template lets say ChildImage template which will contain the Image field. Parent template will have TreeListEx field which will allow to select child items (ChildImage).

Parent Datasource

enter image description here ChildImage

enter image description here

Class

[SitecoreType(TemplateId=I_ImageListConstants.TemplateIdString )] 
public partial interface I_ImageList : IGlassBase 
{
    [SitecoreField(I_ImageListConstants.SlidesFieldName)]
    IEnumerable<_ChildImage> Images  {get; set;}    
}

View

foreach (var i in Images)
{               
    @RenderImage(i, x => x.Image,  isEditable: true)
}
1
  • Thanks for pitching in. Due to certain restrictions, I cannot go for the datasource approach. So, I am going with PageContext. Hence stuck with the Ids of the images. Oct 6, 2019 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.