0

私は次の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 エラーが表示された後でも、データベースからの新しい値を表示する代わりに、古いクライアントの値が表示されるのでしょうか? 何がうまくいかないのかアドバイスできますか?

4

1 に答える 1

2

Reloadデータベースから現在の値を取得する必要はありません。

//...
catch (DbUpdateConcurrencyException ex)
{
    var entry = ex.Entries.Single();
    entry.Reload();
    //...
}
return View(group);
//...

ブラウザで [更新] をクリックすると、POST リクエストの送信が繰り返されます。送信ボタンをもう一度クリックするようなものです。ブラウザー フォームに現在の値がない限り、同じ同時実行例外が再び発生します。URL で Enter キーを押すと GET 要求が送信されるため、GET アクションが呼び出され、現在の値が読み込まれて表示されます。

于 2013-07-13T16:15:45.023 に答える