Skip to main content

template as properties using glass mapper

In many cases your templates won't contain just simple types, they will often link to or contain lists of other template items. For example, you may have a Carousel template that has an Items property. That items property may be a TreeListEx or similar, and will allow the user to choose a list of Carousel Items.

When using GlassMapper to produce a Model for the Carousel. OOTB the Carousel_Items property will likely be an IEnumerable<Guid>. Whilst this may be a valid use case, you will more often than not prefer that property to produce something like IEnumerable<Carousel_Item>.

To achieve this you will need to utilise the Custom Data property under the Code Generation section for your Carousel Item property on the Carousel model.

Follow these steps to transform your list of Guids to a list of models:

  • Identify the property within your TDS project
  • Right click, select properties
  • Find the Custom Data property under the Code Generation section
  • Set the field to type=IEnumerable<Namespace.ModelName>
  • Regenerate models.

You should now find that the regenerated property is mapped to a list of your nested model instead of Guids!