2016-07-07 86 views
0

我有一个模板BrochuresComponent有一个名为位置的字段,这是一个droptree。现在在sitecore模板的字段中使用源代码,用户只能将国家项目或大陆项目添加到此下拉选项。我希望玻璃可以将国家项目映射到ICountry玻璃项目,将大陆项目映射到Icontinent玻璃项目。Sitecore玻璃映射器推测类型Droptree领域与不同的模板项目

但是,当用户在下拉菜单中选择一个选项时,其中一个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. 

空字符串是不允许的。 参数名:字段名

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

回答

3

的infertype是如何工作的你执行/理解是错误的,有Glass tutorial for Inferred Types的读取。

为了这个正常工作,你的国家和大洲的模板需要有一个共同的基础,然后你可以使用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 
    } 
} 

确保从通用基本接口都ICountryIContinent继承相匹配的基础数据模板模型,ILocationBase

+0

另外,请注意,显式指定'TemplateId',正如jammykam在上面的示例中所做的那样,这也是需要的。 –

+0

是否有任何告诫要在包含'IBrochuresComponent'的组件中定义'ILocationBase'接口? –

+0

@MatthewDresser:我不明白为什么它会是一个问题,从来没有尝试过,但是您需要在项目中添加对程序集的引用。 – jammykam

相关问题