简体   繁体   English

在MVC3下使用Telerik控件更新表中的关键字段

[英]Updating key field in table using telerik control under MVC3

I'm using a telerik grid control to manipulate table data under MVC3. 我正在使用Telerik网格控件在MVC3下操纵表格数据。

The key in the table is a field of type string. 表中的键是字符串类型的字段。

Relevant View code: 相关查看代码:

.DataBinding(dataBinding =>
        {
            dataBinding.Ajax()
                .Select("MyTableSelectAjaxEditing", "Maintenance")
                .Insert("MyTableInsertAjaxEditing", "Maintenance")
                .Update("MyTableSaveAjaxEditing", "Maintenance")
                .Delete("MyTableDeleteAjaxEditing", "Maintenance");
        })

My update method is mapped to this function: 我的更新方法映射到此函数:

    [AcceptVerbs(HttpVerbs.Post)]
    [GridAction]
    public ActionResult MyTableSaveAjaxEditing(String id)
    {
        MyTable myTable = db.MyTable.Find(id);

        if (myTable != null)
        {
            if (TryUpdateModel(myTable))
            {
                db.SaveChanges();
            }
        }
        return View(new GridModel(db.MyTable));
    }

All is well when I update fields that are not the key. 当我更新不是关键的字段时,一切都很好。 When the key is being updated (from the grid), the id parameter returns the NEW value, so the Find function at the beginning of the method can't find the relevant record and the update does not take place! 当更新键(从网格)时,id参数返回NEW值,因此方法开始处的Find函数找不到相关记录,并且更新不会发生!

Any assistance will be highly appreciated. 任何帮助将不胜感激。

Thank you. 谢谢。

My only recommendation is creating a hidden column with the old Id and return that along with the other data and new Id. 我唯一的建议是使用旧ID创建一个隐藏列,并将其与其他数据和新ID一起返回。 I have done this similarly in the past. 我过去也这样做过。 Of course you would have to add the old Id to the view model. 当然,您必须将旧ID添加到视图模型中。
Not the most elegant solution ever but that is what you have to deal with when the key can change. 这不是有史以来最优雅的解决方案,但这是密钥可以更改时必须处理的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM