我正在使用Glass Mapper:4.0.1.8将多列表映射到Sitecore 8中的IEnumberable
。它确实正确返回IEnumberable
中的计数,但内部的字段都映射到空值。以下是我正在使用的代码:
public void GetPromotedNews()
{
var NewsListingPage = Sitecore.Context.Database.GetItem(new ID(ItemIDS.NewsListingCorporate));
var NewsListingPageItem = NewsListingPage.GlassCast<NewsListingPage>();
foreach (var newslistingcategory in NewsListingPageItem.NewsCategory) //This is returning two results which are correct
{
var item = newslistingcategory.CategoryName; //this is always returning null ?
}
}
public class NewsListingPage
{
[SitecoreField(FieldName = "Page title")]
public string PageTitle { get; set; }
[SitecoreField(FieldName = "Page description")]
public string PageDescription {get; set;}
[SitecoreField(FieldName = "News category", Setting = SitecoreFieldSettings.InferType)]
public virtual IEnumerable<ContentCategory> NewsCategory { get; set; }
}
[SitecoreType(TemplateId = DCP.Resources.TemplateIDS.CategoryTemplateID, AutoMap = true)]
public class ContentCategory : SCItem
{
[SitecoreField(FieldName = "Category name")]
public string CategoryName { get; set; }
[SitecoreField(FieldName = "Category icon")]
public Image CategoryICON { get; set; }
[SitecoreField(FieldName = "text")]
public string Text { get; set; }
}
答案 0 :(得分:2)
尝试更新您的内容类别模型以获得虚拟属性
[SitecoreType(TemplateId = DCP.Resources.TemplateIDS.CategoryTemplateID, AutoMap = true)]
public class ContentCategory : SCItem
{
[SitecoreField(FieldName = "Category name")]
public virtual string CategoryName { get; set; }
[SitecoreField(FieldName = "Category icon")]
public virtual Image CategoryICON { get; set; }
[SitecoreField(FieldName = "text")]
public virtual string Text { get; set; }
}
答案 1 :(得分:0)
Making them virtual应该做到这一点。
使用GlassMapper遇到null的另一个原因是它无法解析您尝试映射到的类型。例如,如果我将类型设置为string
,但它实际上是List<string>
。我也推荐TDS。您可以获取控制TDS项目并自动生成您的类。
答案 2 :(得分:0)
当我遇到这个问题时,有两个可能的原因。
我在我的字段中添加了以下属性。
[SitecoreField(Setting = Glass.Mapper.Sc.Configuration.SitecoreFieldSettings.DontLoadLazily)]