public ActionResult Create(Department department) { if (ModelState.IsValid) { db.Departments.Add(department); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.InstructorID = new SelectList(db.Instructors, "UserID", "InstructorName", department.UserID); return View(department); }
public ActionResult DeleteConfirmed(Department department) { try { db.Entry(department).State = EntityState.Deleted; db.SaveChanges(); return RedirectToAction("Index"); } catch (DbUpdateConcurrencyException) { return RedirectToAction("Delete", new System.Web.Routing.RouteValueDictionary { { "concurrencyError", true } }); } catch (DataException) { //Log the error (add a variable name after Exception) ModelState.AddModelError(string.Empty, "Unable to save changes."+ " Try again, and if the problem persists contact your system administrator."); return View(department); } }
public ActionResult Edit(Department department) { try { if (ModelState.IsValid) { db.Entry(department).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } } catch (DbUpdateConcurrencyException ex) { var entry = ex.Entries.Single(); var databaseValues = (Department)entry.GetDatabaseValues().ToObject(); var clientValues = (Department)entry.Entity; if (databaseValues.DepartmentName != clientValues.DepartmentName) ModelState.AddModelError("Name", "Current value: " + databaseValues.DepartmentName); if (databaseValues.UserID != clientValues.UserID) ModelState.AddModelError("InstructorID", "Current value: " + db.Instructors.Find(databaseValues.UserID).InstructorName); 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; } catch (DataException) { //Log the error (add a variable name after Exception) ModelState.AddModelError(string.Empty, "Unable to save changes."+ " Try again, and if the problem persists contact your system administrator."); } ViewBag.InstructorID = new SelectList(db.Instructors, "UserID", "InstructorName", department.UserID); return View(department); }