Sitecore Glass Mapper Infer Type Droptree Field with different template items

365 views
Skip to first unread message

Kasturi Chavan

unread,
Jul 7, 2016, 1:46:09 PM7/7/16
to Glass.Sitecore.Mapper
I have a template BrochuresComponent that has a field named Location which is a droptree. Now using source on the field in sitecore template, a user is only going to be able to add a country item or a continent item to this dropdown option. I want glass to map the country item in option to a ICountry glass Item and the continent Item to a Icontinent glass item. 

But when user selects a option in dropdown,one of the glassItem value is filled while other has evaluation errors on it resulting in error on the model. Below is my code snippet. 

My glass Interface looks as follows:
 
        using Collette.Library.GlassItems.Objects.Taxonomy.Locations;
        using   Collette.Library.GlassItemsConstants.Objects.Page_Components.Destination_Page_Components;
        using Glass.Mapper.Sc.Configuration;
        using Glass.Mapper.Sc.Configuration.Attributes;

        namespace Collette.Library.GlassItems.Objects.Page_Components.Destination_Page_Components
        {
         [SitecoreType(TemplateId = BrochureComponentsConstants.TemplateIdString, AutoMap = true)]
         public interface IBrochuresComponent: IGlassItemEx
         {
              //Brochures Data Section
              [SitecoreField(FieldType = SitecoreFieldType.DropTree, FieldId = BrochureComponentsConstants.LocationItemFieldId, Setting = SitecoreFieldSettings.InferType)]
              ICountry Country { get; set; }

              [SitecoreField(FieldType = SitecoreFieldType.DropTree, FieldId = BrochureComponentsConstants.LocationItemFieldId, Setting = SitecoreFieldSettings.InferType)]
              IContinent Continent { get; set; }

       }
    }

My Model Looks as follows:
        
      var sitecoreContext = new SitecoreContext();
      BrochuresComponentItem = sitecoreContext.Cast<IBrochuresComponent>(DisplayItem); 
      //IF we specified a continent in dropdown it works fine since that is the first condition so it does not go to else,
      //but if we specify a country in the dropdown it breaks at the if condition below stating that templateid is empty string since nothing was evaluated.

Empty strings are not allowed.
Parameter name: fieldName
 
      if (BrochuresComponentItem.Continent != null && BrochuresComponentItem.Continent.TemplateId.Equals(ContinentConstants.TemplateId))
       {
          SetBrochures(BrochuresComponentItem.Continent);
       }
       else if (BrochuresComponentItem.Country != null && BrochuresComponentItem.Country.TemplateId.Equals(CountryConstants.TemplateId))
       {
         SetBrochures(BrochuresComponentItem.Country);
       }

Reply all
Reply to author
Forward
0 new messages