2016-07-07 33 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

相關問題