public async Task <IActionResult> Create([Bind("ID,FirstName,LastName,Email,Phone")] ProgramSupervisor programSupervisor) { try { if (ModelState.IsValid) { _context.Add(programSupervisor); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException dex) { if (dex.InnerException.Message.Contains("IX_ProgramSupervisors_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(programSupervisor)); }
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)); }