私は次のGETアクションメソッドを持っています:-
public ActionResult Edit(int id)
{
return View(groupRepository.Find(id));
}
私は次のPOSTアクションメソッドを持っています:-
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Group group)
{
try
{
if (ModelState.IsValid)
{
AuditInfo auditinfo = repository.IntiateAudit(2, 2, User.Identity.Name, 2);
groupRepository.InsertOrUpdate(group);
groupRepository.Save();
repository.InsertOrUpdateAudit(auditinfo);
return RedirectToAction("Index");
}
}
catch (DbUpdateConcurrencyException ex)
{
var entry = ex.Entries.Single();
var clientValues = (Group)entry.Entity;
ModelState.AddModelError(string.Empty, "The record you attempted to edit "
+ "was modified by another user after you got the original value. The "
+ "edit operation was canceled and the current values in the database "
+ "have been displayed. If you still want to edit this record, click "
+ "the Save button again. Otherwise click the Back to List hyperlink."); }
しかし問題は、(DbUpdateConcurrencyException) が発生した場合、ユーザーがページを更新した後も ModelState エラーが表示され続けることです。
2 番目の問題は、更新後、データベースから更新された値を表示するのではなく、古い値が表示され続けることです。
ただし、ブラウザの URL をクリックして [Enter] をクリックすると、エラーが削除され、ページを更新するのとは異なり、値がデータベースから取得されます。
最後に、Find メソッドは次のとおりです:-
public Group Find(int id)
{ return context.Groups.Find(id) ;}
::編集::
POST EDITアクションメソッドを次のように更新しました:-
catch (DbUpdateConcurrencyException ex)
{
var entry = ex.Entries.Single();
var databaseValues = (Group)entry.GetDatabaseValues().ToObject();
entry.Reload();
var clientValues = (Group)entry.Entity;
ModelState.AddModelError(string.Empty, "The record you attempted to edit "
+ "was modified by another user after you got the original value. The "
+ "edit operation was canceled and the current values in the database "
+ "have been displayed. If you still want to edit this record, click "
+ "the Save button again. Otherwise click the Back to List hyperlink.");
// department.Timestamp = databaseValues.Timestamp;
group.timestamp = databaseValues.timestamp;
しかし、ModelState エラーが表示された後でも、データベースからの新しい値を表示する代わりに、古いクライアントの値が表示されるのでしょうか? 何がうまくいかないのかアドバイスできますか?