Asp.net Sitecore Glass Mapper推断类型Droptree字段具有不同的模板项

Asp.net Sitecore Glass Mapper推断类型Droptree字段具有不同的模板项,asp.net,sitecore,sitecore7,sitecore7.2,glass-mapper,Asp.net,Sitecore,Sitecore7,Sitecore7.2,Glass Mapper,我有一个模板手册组件,它有一个名为Location的字段,它是一个droptree。现在使用sitecore模板中字段上的source,用户只能将国家项目或大陆项目添加到此下拉选项中。我希望glass将选项中的国家项目映射到ICountry玻璃项目,将大陆项目映射到Icontinent玻璃项目 但当用户在下拉列表中选择一个选项时,其中一个glassItem值被填充,而另一个值上有计算错误,从而导致模型错误。下面是我的代码片段 我的玻璃界面如下所示: using Collette.Libr

我有一个模板手册组件,它有一个名为Location的字段,它是一个droptree。现在使用sitecore模板中字段上的source,用户只能将国家项目或大陆项目添加到此下拉选项中。我希望glass将选项中的国家项目映射到ICountry玻璃项目,将大陆项目映射到Icontinent玻璃项目

但当用户在下拉列表中选择一个选项时,其中一个glassItem值被填充,而另一个值上有计算错误,从而导致模型错误。下面是我的代码片段

我的玻璃界面如下所示:

    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; }

   }
}
  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.
我的模型如下所示:

    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; }

   }
}
  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.

您对infertype工作原理的实现/理解是错误的,请阅读

为了使其正常工作,您的国家/地区和大陆模板需要有一个公共基础,然后您可以使用infertype映射到特定类型:

 [SitecoreType(TemplateId = BrochureComponentsConstants.TemplateIdString, AutoMap = true)]
 public interface IBrochuresComponent: IGlassItemEx
 {
      //Brochures Data Section
      [SitecoreField(FieldType = SitecoreFieldType.DropTree, FieldId = BrochureComponentsConstants.LocationItemFieldId, Setting = SitecoreFieldSettings.InferType)]
      ILocationBase Location { get; set; }
}
然后,您可以在代码上检查其映射到的类型:

if (BrochuresComponentItem.Location != null)
{
    if (BrochuresComponentItem.Location is ICountry)
    {
        //do country specific thing
    }
    else if (BrochuresComponentItem.Location is IContinent)
    {
        // do continent specific thing
    }
}

确保
ICountry
IContinent
的模型都继承自公共基础接口,以匹配基础数据模板,
ILocationBase

如果您对推断类型工作原理的实现/理解有误,请阅读

为了使其正常工作,您的国家/地区和大陆模板需要有一个公共基础,然后您可以使用infertype映射到特定类型:

 [SitecoreType(TemplateId = BrochureComponentsConstants.TemplateIdString, AutoMap = true)]
 public interface IBrochuresComponent: IGlassItemEx
 {
      //Brochures Data Section
      [SitecoreField(FieldType = SitecoreFieldType.DropTree, FieldId = BrochureComponentsConstants.LocationItemFieldId, Setting = SitecoreFieldSettings.InferType)]
      ILocationBase Location { get; set; }
}
然后,您可以在代码上检查其映射到的类型:

if (BrochuresComponentItem.Location != null)
{
    if (BrochuresComponentItem.Location is ICountry)
    {
        //do country specific thing
    }
    else if (BrochuresComponentItem.Location is IContinent)
    {
        // do continent specific thing
    }
}

确保
ICountry
IContinent
的模型都继承自公共基础接口,以匹配基础数据模板,
ILocationBase

。此外,请注意,如上面的jammykam示例所述,显式指定
模板ID
,也需要这样做。在不同的程序集中定义
ILocationBase
接口与包含
iRochuresComponent
的程序集中定义接口是否有任何注意事项?@MatthewDresser:我不明白为什么会有问题,我自己从未尝试过,但您需要在项目中添加对该程序集的引用。另外,请注意,如上面jammykam在其示例中所做的那样,显式指定
TemplateId
,也需要这样做。在包含
iRochuresComponent
的程序集中定义
ILocationBase
接口时,是否有任何注意事项?@MatthewDresser:我不明白为什么会出现问题,我自己从未尝试过,但您需要在项目中添加对程序集的引用。