我有一个模板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