我有一個模板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);
}
另外,請注意,顯式指定'TemplateId',正如jammykam在上面的示例中所做的那樣,這也是需要的。 –
是否有任何告誡要在包含'IBrochuresComponent'的組件中定義'ILocationBase'接口? –
@MatthewDresser:我不明白爲什麼它會是一個問題,從來沒有嘗試過,但是您需要在項目中添加對程序集的引用。 – jammykam