【发布时间】:2013-09-18 07:48:00
【问题描述】:
我无法从我在迁移中动态创建并附加到内容类型的附加部分的 DateTimeField 获取 DateTime。
结构是这样的:
CourseDate (ContentType)
ProductPart (attached part from other project)
CourseDatePart (attached part)
TimeSpanPart (attached part from "dynamically" created part)
我正在尝试将 StartDateTime 和 EndDateTime(它们是 DateTimeField)从 TimeSpanPart 中提取出来。但是 DateTime 属性始终是 1/1/0001
这是我用来提取值的代码。除了 TimeSpanPart 之外,其他一切都有效。
private EditCourseViewModel BuildEditorViewModel(CoursePart part)
{
var courseDates = _contentManager.Query<CourseDatePart, CourseDatePartRecord>().Where(x => x.CourseId == part.Id)
.List<CourseDatePart>()
.Select(x =>
new CourseDateViewModel
{
Id = x.Id,
StartDate = ((dynamic) x.ContentItem).TimeSpanPart.StartDateTime.DateTime,
EndDate = ((dynamic) x.ContentItem).TimeSpanPart.EndDateTime.DateTime,
Sku = ((dynamic) x.ContentItem).ProductPart.Sku,
IsDigital = ((dynamic) x.ContentItem).ProductPart.IsDigital,
Inventory = x.Inventory
}
);
当我检查 x.Record.ContentItemRecord.Data 的值时,我可以看到我想要的数据在那里:
<Data><TimeSpanPart><StartDateTime>2013-09-02T06:20:00.0000000</StartDateTime><EndDateTime>2013-09-21T00:05:00.0000000</EndDateTime></TimeSpanPart></Data>
以下是迁移的相关部分:
ContentDefinitionManager.AlterPartDefinition("TimeSpanPart", part=>part
.Attachable()
.WithField("StartDateTime", f=>f.OfType("DateTimeField").WithDisplayName("Start Date Time"))
.WithField("EndDateTime", f=>f.OfType("DateTimeField").WithDisplayName("End Date Time"))
);
ContentDefinitionManager.AlterTypeDefinition("CourseDate", type=>type
.WithPart(typeof(CourseDatePart).Name)
.WithPart("TimeSpanPart")
.WithPart(typeof(ProductPart).Name)
);
编辑:
我更深入地研究了 Orchard InfosetStorageProvider 类,它管理字段的存储。在 BindStorage 方法中,它将 Data XML 元素传递给 Get 方法。但不是传递 infosetPart.Infoset.Element (它具有我想要在上面显示的数据),而是传递 infosetPart.ContentItem.VersionRecord 因为它不为空;它是一个空的 XML 元素:
return new SimpleFieldStorage(
(name, valueType) => Get(infosetPart.ContentItem.VersionRecord == null ? infosetPart.Infoset.Element : infosetPart.VersionInfoset.Element, partName, fieldName, name),
(name, valueType, value) => Set(infosetPart.ContentItem.VersionRecord == null ? infosetPart.Infoset.Element : infosetPart.VersionInfoset.Element, partName, fieldName, name, value));
}
如何让它正确地将数据填充到 infosetPart.ContentItem.VersionRecord 中? 这是 Orchard 中的一个错误,因为它传递了一个空的 xml 元素而不是 null,因此它会将 infosetPart.Infoset.Element 传递给 Get 方法,还是我做错了什么?我不知道 infosetPart.ContentItem.VersionRecord 和 infosetPart.Infoset.Element 之间有什么区别。
【问题讨论】:
-
怎么不行?
-
例如,对于字段 StartDateTime.DateTime,我期待 2013-09-02T06:20:00,但我得到的是 1/1/0001。你能看出我正在尝试做的事情有什么问题吗?
标签: orchardcms