我在Orchard中创建了一个自定义字段,它只包含一个字段(GUID)。因此,无论何时将其添加到内容类型,它都会在“编辑器”模板中显示新的GUID。这工作正常,除非我提交包含内容类型的表单,表单将使用不同的GUID发布。仔细观察,我发现编辑器(POST)字段的驱动程序不会更新该字段的viewModel。
有任何调试建议或提示吗?
以下是我的代码的一些行
protected override DriverResult Display(
ContentPart part, Fields.UniqueIDField field,
string displayType, dynamic shapeHelper)
{
return ContentShape("Fields_String", // this is just a key in the Shape Table
GetDifferentiator(field, part),
() =>
{
var settings = field.PartFieldDefinition.Settings.GetModel<UniqueIdFieldSettings>();
var value = field.Id;
var viewModel = new UniqueIdFieldViewModel
{
Id = value
};
return shapeHelper.Fields_String( // this is the actual Shape which will be resolved (Fields/DateTime.cshtml)
Model: viewModel);
}
);
}
protected override DriverResult Editor(ContentPart part,
Fields.UniqueIDField field,
dynamic shapeHelper)
{
var settings = field.PartFieldDefinition.Settings
.GetModel<UniqueIdFieldSettings>();
var value = Guid.NewGuid().ToString();
field.Id = value;
var viewModel = new UniqueIdFieldViewModel
{
Id = value,
};
return ContentShape("Fields_String_Edit",
() => shapeHelper.EditorTemplate(
TemplateName: TemplateName,
Model: viewModel,
Prefix: GetPrefix(field, part)));
}
protected override DriverResult Editor(ContentPart part,
Fields.UniqueIDField field,
IUpdateModel updater,
dynamic shapeHelper)
{
var viewModel = new UniqueIdFieldViewModel();
if (updater.TryUpdateModel(viewModel,
GetPrefix(field, part), null, null))
{
field.Id = viewModel.Id;
}
return Editor(part, field, shapeHelper);
}
答案 0 :(得分:1)
每次在GET编辑器方法中重置值,每次在自己执行结束时由POST编辑器方法调用。只有在没有值的情况下才设置为新值。