Map Droplist item to complextype using Glass.Mapper

By : Rene
Source: Stackoverflow.com
Question!

I have created two Sitecore templatates MenuItem and MainNavigation. I have created two interfaces wich are based on these templates:

 [SitecoreType(TemplateId = "{C824E484-F4A6-475C-AFAF-308FF4BBA5A9}", AutoMap = true)]
public interface IMenuItem
{
    string Title { get; set; }
    IEnumerable<IMenuItem> SubMenuItems { get; set; }
}
[SitecoreType(TemplateId = "{68947CC0-7658-4188-889D-4E88B84F3BC2}", AutoMap = true)]
public interface IMainNavigation
{        
    IMenuItem MenuHeaderItem { get; set; }

    IEnumerable<IMenuItem> MenuItems { get; set; }
}

The mapping MenuItems from an MultiList is working. The mapping MenuHeaderItem from an Droplist is not. In the template I have provided an query in the datasource so only MenuItems can be selected.

How can I make this mapping work?

I've tried v4.0.5.54 and now I have updated to version 4.2.1.188

By : Rene


Answers

I have found the answer myself. In the template I have to use sitecore type Droplink instead of DropList.

Droplist only stores the selected item name as a string. Droplink also strores the GUID.

By : Rene


What if you try to put this Annotation (see below) for the property that is not being populated:

[SitecoreField(FieldName = "MenuHeaderItem", FieldType = SitecoreFieldType.Droplist)]


I'd do it with zip and list comprehension.

>>> a = [2, 4, 1, 1, 6, 1, 1, 3, 5, 1]
>>> b = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
>>> c = [x[0] for x in zip(b, a) if x[1] == 1]
>>> c
['C', 'D', 'F', 'G', 'J']
>>>


This video can help you solving your question :)
By: admin