public async Task <IActionResult> Edit(int id, Byte[] RowVersion, [Bind("ID,ProgramName,ProgramDescription,ProgramJoinCode,ProgramSupervisorID")] Models.Program program) { var programToUpdate = await _context.Programs.SingleOrDefaultAsync(p => p.ID == id); if (programToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Models.Program>(programToUpdate, "", p => p.ProgramName, p => p.ProgramDescription, p => p.ProgramJoinCode, p => p.ProgramSupervisorID)) { try { _context.Entry(programToUpdate).Property("RowVersion").OriginalValue = RowVersion; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (DbUpdateConcurrencyException dex) { var exceptionEntry = dex.Entries.Single(); var clientValues = (Models.Program)exceptionEntry.Entity; var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError("", "Unable to save changes. The Program was deleted by another user."); } else { var databaseValues = (Models.Program)databaseEntry.ToObject(); if (databaseValues.ProgramName != clientValues.ProgramName) { ModelState.AddModelError("ProgramName", "Current value: " + databaseValues.ProgramName); } if (databaseValues.ProgramDescription != clientValues.ProgramDescription) { ModelState.AddModelError("ProgramDescription", "Current value: " + databaseValues.ProgramDescription); } if (databaseValues.ProgramJoinCode != clientValues.ProgramJoinCode) { ModelState.AddModelError("ProgramJoinCode", "Current value: " + databaseValues.ProgramJoinCode); } if (databaseValues.ProgramSupervisorID != clientValues.ProgramSupervisorID) { ProgramSupervisor databaseAssignment = await _context.ProgramSupervisors.SingleOrDefaultAsync(i => i.ID == databaseValues.ProgramSupervisorID); ModelState.AddModelError("ProgramSupervisorID", $"Current value: {databaseAssignment?.Supervisor}"); } ModelState.AddModelError(string.Empty, "The record you attempted to edit " + "was modified by another user after you received your values. The " + "edit operation was canceled and the current values in the database " + "have been displayed. If you still want to save your version of this record, click " + "the Save button again. Otherwise click the 'Back to List' hyperlink."); programToUpdate.RowVersion = (byte[])databaseValues.RowVersion; ModelState.Remove("RowVersion"); } } catch (DbUpdateException dex) { if (dex.InnerException.Message.Contains("IX_Programs_ProgramJoinCode")) { ModelState.AddModelError("Email", "Unable to save changes. Remember, you cannot have duplicate Program Join Codes."); } else { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } } } ViewData["ProgramSupervisorID"] = new SelectList(_context.ProgramSupervisors, "ID", "Supervisor", program.ProgramSupervisorID); return(View(program)); }
public async Task <IActionResult> Edit(int id, Byte[] RowVersion, [Bind("ID,FirstName,LastName,Email,Phone,NotificationOptIn")] Parent parent) { var parentToUpdate = await _context.Parents.SingleOrDefaultAsync(p => p.ID == id); if (parentToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Parent>(parentToUpdate, "", p => p.FirstName, p => p.LastName, p => p.Email, p => p.Phone, p => p.NotificationOptIn)) { try { _context.Entry(parentToUpdate).Property("RowVersion").OriginalValue = RowVersion; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (DbUpdateConcurrencyException dex) { var exceptionEntry = dex.Entries.Single(); var clientValues = (Parent)exceptionEntry.Entity; var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError("", "Unable to save changes. The Parent was deleted by another user."); } else { var databaseValues = (Parent)databaseEntry.ToObject(); if (databaseValues.FirstName != clientValues.FirstName) { ModelState.AddModelError("FirstName", "Current value: " + databaseValues.FirstName); } if (databaseValues.LastName != clientValues.LastName) { ModelState.AddModelError("LastName", "Current value: " + databaseValues.LastName); } if (databaseValues.Email != clientValues.Email) { ModelState.AddModelError("Email", "Current value: " + databaseValues.Email); } if (databaseValues.Phone != clientValues.Phone) { ModelState.AddModelError("Phone", "Current value: " + String.Format("{0:(###) ###-####}", databaseValues.Phone)); } ModelState.AddModelError(string.Empty, "The record you attempted to edit " + "was modified by another user after you received your values. The " + "edit operation was canceled and the current values in the database " + "have been displayed. If you still want to save your version of this record, click " + "the Save button again. Otherwise click the 'Back to List' hyperlink."); parentToUpdate.RowVersion = (byte[])databaseValues.RowVersion; ModelState.Remove("RowVersion"); } } catch (DbUpdateException dex) { if (dex.InnerException.Message.Contains("IX_Parents_Email")) { ModelState.AddModelError("Email", "Unable to save changes. Remember, you cannot have duplicate Email Addresses."); } else { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system admin."); } } } return(View(parent)); }