我通过 Glass.Mapper 创建一个 Sitecore 项目,如下所示:
var homeItem = sitecoreContext.GetHomeItem<HomeItem>();
// Create the car item
ICar car = sitecoreService.Create(homeItem.BooksFolder, new Car { Tires = 4, Seats=4});
这是有效的,除了 Car 模板上的标准值没有被应用 - 或者如果它们被新的 Car 属性立即覆盖。因此,如果 Car 对象的 Color 属性值为 null,则此 null 将被写入字段,而不是 Car 模板上标准值的“绿色”值。
我一直在寻找通过 Glass.Mapper 执行此操作的明智方法,但一无所获。 有没有办法通过 Glass.Mapper 做到这一点?
最佳答案
有一种方法可以做到这一点,使用看起来像这样的 Create 方法的覆盖:
T Create<T, TK>(TK parent, string newName, Language language = null, bool updateStatistics = true, bool silent = false) where T : class where TK : class;
所以你的代码看起来像这样:
var homeItem = sitecoreContext.GetHomeItem<HomeItem>();
var carName = "Some New Name";
// Create the car item
// I don't know what the type of BooksFolder is so you would put that in the place of Folder.
ICar car = sitecoreService.Create<Car, Folder>(homeItem.BooksFolder, carName);
car.Tires = 4;
car.Seats = 4;
sitecoreService.Save(car);
我们遇到了同样的问题,这就是我们解决它的方法。
关于c# - 如何将标准值应用于使用 Glass.Mapper 创建的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26674102/