具有不同模板项的 Sitecore Glass Mapper 推断类型 Droptree 字段
Sitecore Glass Mapper Infer Type Droptree Field with different template items
我有一个模板 BrochuresComponent,它有一个名为 Location 的字段,它是一个 droptree。现在在 sitecore 模板的字段上使用源代码,用户只能将国家项目或大陆项目添加到此下拉选项。我希望玻璃将选项中的国家项目映射到 ICountry 玻璃项目,将大陆项目映射到 Icontinent 玻璃项目。
但是当用户在下拉列表中选择一个选项时,其中一个 glassItem 值被填充,而其他值存在评估错误,导致模型出现错误。下面是我的代码片段。
我的 glass 界面如下所示:
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.
不允许空字符串。
参数名称:fieldName
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);
}
您关于推断类型工作方式的 implementation/understanding 是错误的,请阅读 Glass tutorial for Inferred Types。
为了使其正常工作,您的 Country 和 Continent 模板需要有一个共同的基础,然后您可以使用 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
}
}
确保 ICountry
和 IContinent
的模型继承自公共基础接口以匹配基础数据模板,ILocationBase
我有一个模板 BrochuresComponent,它有一个名为 Location 的字段,它是一个 droptree。现在在 sitecore 模板的字段上使用源代码,用户只能将国家项目或大陆项目添加到此下拉选项。我希望玻璃将选项中的国家项目映射到 ICountry 玻璃项目,将大陆项目映射到 Icontinent 玻璃项目。
但是当用户在下拉列表中选择一个选项时,其中一个 glassItem 值被填充,而其他值存在评估错误,导致模型出现错误。下面是我的代码片段。
我的 glass 界面如下所示:
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.
不允许空字符串。 参数名称:fieldName
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);
}
您关于推断类型工作方式的 implementation/understanding 是错误的,请阅读 Glass tutorial for Inferred Types。
为了使其正常工作,您的 Country 和 Continent 模板需要有一个共同的基础,然后您可以使用 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
}
}
确保 ICountry
和 IContinent
的模型继承自公共基础接口以匹配基础数据模板,ILocationBase