Вопрос

I have the following GET Action method:-

public ActionResult Edit(int id)
        {
             return View(groupRepository.Find(id));
        }

I have the following POST Action method:-

[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."); }

But the problem is that in case the (DbUpdateConcurrencyException) is raised then the ModelState error will keep displaying, after the user refresh the page.

Second problem is that after the refresh the old values will keep displaying, instead of viewing the updated values from the database.

But in case i click on the browser URL and click "Enter" then the error will be removed, and the values will be retrieved from the Database ,unlike refreshing the page.

Finally the Find method is :-

    public Group Find(int id)
            { return context.Groups.Find(id) ;}

::EDIT::

I have updated my POST EDIT action method to be:-

   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;

But still after the ModelState error is displayed the old client values will still be displayed , instead of displaying the fresh values from the database ? can you advice on what might be going wrong?

Это было полезно?

Решение

There is no Reload to fetch the current values from the database:

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

If you click on Refresh in the browser it will repeat to send a POST request. It's like clicking the submit button again. As long as you don't have the current values in the browser form you will hit the same concurrency exception again. Hitting Enter on the Url will send a GET request, so your GET action will be called and the current values loaded and displayed.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top