【问题标题】:Using Umbraco Forms to edit data使用 Umbraco Forms 编辑数据
【发布时间】:2015-08-31 19:56:35
【问题描述】:

我想使用 Umbraco Forms 不仅可以插入数据,还可以对其进行编辑。到目前为止,当我想编辑一条记录时,我正在通过查询字符串传递表单 guid 和记录 id,并在字段中填充正确的数据。 到目前为止一切顺利。

然后我像这样成功连接到 Umbraco.Forms.Data.Storage.RecordStorage.RecordInserting 事件

void RecordStorage_RecordInserting(object sender, Umbraco.Forms.Core.RecordEventArgs e)
    {
        var ms = (Umbraco.Forms.Data.Storage.RecordStorage)sender;
        if(this record exists){
         ms.UpdateRecord(e.Record, e.Form);
        }
    }

但是,当我尝试提交已编辑的记录并运行 ms.RecordUpdate(e.Record, e.Form) 行时,我收到此错误

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_UFRecordDataString_UFRecordFields_Key". The conflict occurred in database "UmbracoPlay", table "dbo.UFRecordFields", column 'Key'.
The statement has been terminated.

我不能删除旧记录然后插入新记录,因为每次我调用 ms.InsertRecord 时它都会重新引发相同的事件

我错过了什么? 如何使用 Umbraco Forms 编辑现有数据?

【问题讨论】:

  • 如果您正在编辑数据,我认为您不需要挂钩插入事件。您使用的是哪个版本的 Umbraco/Contour?

标签: umbraco umbraco7 umbraco-contour


【解决方案1】:

我看不到此错误的修复 - 似乎 UpdateRecord 方法实际上尝试第二次插入所有 UFRecordField 对象,而不是更新现有值(或现有字段值)导致此密钥违规。

如果您真的需要解决这个问题 - 就像我所做的那样 - 那么可行的一件事(但会给您留下更多碎片化的主键)就是删除然后重新插入表单数据:

    var ms = (Umbraco.Forms.Data.Storage.RecordStorage)sender;
    if(this record exists){
       ms.DeleteRecord(e.Record, e.Form);
       ms.InsertRecord(e.Record, e.Form);
    }

一个杂乱无章的解决方案,但看似有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多