我有以下操作方法:-
[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();
entry.Reload();
ModelState.Clear();
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.");
}
catch (DbUpdateException)
{
ModelState.AddModelError(string.Empty, " An Error Occured, please check the Group name for uniquenee.");
}
catch (DataException) {
ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator.");
}
return View(group);
}
目前,上面的代码可以正常工作,但我不确定在异常中编写.Reload()
&方法是一种好习惯还是被认为是一种不好的编程习惯?.Clear()
谢谢!