2016-07-07 9 views
0

私はdroptreeというLocationという名前のフィールドを持つテンプレートBrochuresComponentを持っています。 sitecoreテンプレートのフィールドのソースを使用すると、ユーザーはこのドロップダウンオプションに国のアイテムまたは大陸のアイテムを追加できるようになります。私はガラスにオプションの国のアイテムをICountryのガラスアイテムに、大陸のアイテムをIcontinentのガラスアイテムにマッピングしたい。Sitecore Glass Mapper Infer Type Droptree異なるテンプレート項目のフィールド

しかし、ユーザーがドロップダウンでオプションを選択すると、glassItem値の1つが塗りつぶされ、他の評価エラーがあるためモデルにエラーが発生します。以下は私のコードスニペットです。

次のように私のガラスインタフェースになります。次のように

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

ILocationBase

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

が基本データテンプレートに一致するように、共通の基本インタフェースの両方からICountryIContinent継承のためのモデルを確認してください:それはにマッピングされている種類を確認することができます

+0

また、上記の例ではjammykamが明示的に指定したように、これが動作するためにも 'TemplateId'を指定する必要があります。 –

+0

'IBrochuresComponent'を含むアセンブリとは異なるアセンブリの' ILocationBase'インタフェースを定義する際に注意すべき点はありますか? –

+0

@MatthewDresser:なぜそれが問題になるのかわかりません。自分で試したことはありませんが、プロジェクトのアセンブリへの参照を追加する必要があります。 – jammykam

関連する問題