Reputation: 133
I'm using a telerik grid control to manipulate table data under 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!
Any assistance will be highly appreciated.
Thank you.
Upvotes: 0
Views: 202
Reputation: 928
My only recommendation is creating a hidden column with the old Id and return that along with the other data and new Id. I have done this similarly in the past.
Of course you would have to add the old Id to the view model.
Not the most elegant solution ever but that is what you have to deal with when the key can change.
Upvotes: 1