コード例 #1
0
        public ActionResult EditPost(int?id, string[] selectedCourses)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var instructorToUpdate = db.Instructors
                                     .Include(i => i.OfficeAssignment)
                                     .Include(i => i.Courses)
                                     .Where(i => i.ID == id)
                                     .Single();

            if (TryUpdateModel(instructorToUpdate, "", new string[] { "LastName", "FirstMidName", "HireDate", "OfficeAssignment" }))
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(instructorToUpdate.OfficeAssignment.Location))
                    {
                        instructorToUpdate.OfficeAssignment = null;
                    }
                    UpdateInstructorCourses(selectedCourses, instructorToUpdate);
                    db.Entry(instructorToUpdate).State = EntityState.Modified;
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (RetryLimitExceededException)
                {
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                }
            }
            PopulateAssignedCourseData(instructorToUpdate);
            return(View(instructorToUpdate));
        }
コード例 #2
0
 public ActionResult Edit([Bind(Include = "ID,LastName,FirstMidName,EnrollmentDate")] Student student)
 {
     try
     {
         if (ModelState.IsValid)
         {
             db.Entry(student).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
     }
     catch (DataException)
     {
         ModelState.AddModelError("", "Unable to save chages.");
         throw;
     }
     return(View(student));
 }
コード例 #3
0
 public ActionResult Edit([Bind(Include = "CourseID,Title,Credits,DepartmentID")] Course course)
 {
     try
     {
         if (ModelState.IsValid)
         {
             db.Entry(course).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
     }
     catch (DataException)
     {
         ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
     }
     PopulateDepartmentsDropDownList(course.DepartmentID);
     return(View(course));
 }
コード例 #4
0
        public async Task <ActionResult> Edit([Bind(Include = "DepartmentID, Name, Budget, StartDate, RowVersion,InstructorID")] Department department)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    ValidateOneAdministratorAssignmentPerInstructor(department);
                }
                if (ModelState.IsValid)
                {
                    db.Entry(department).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }
            catch (DbUpdateConcurrencyException ex)
            {
                var entry         = ex.Entries.Single();
                var clientValues  = (Department)entry.Entity;
                var databaseEntry = entry.GetDatabaseValues();
                if (databaseEntry == null)
                {
                    ModelState.AddModelError(string.Empty,
                                             "Unable to save changes. The department was deleted by another user.");
                }
                else
                {
                    var databaseValues = (Department)databaseEntry.ToObject();
                    if (databaseValues.Name != clientValues.Name)
                    {
                        ModelState.AddModelError("Name", "Current value: "
                                                 + databaseValues.Name);
                    }
                    if (databaseValues.Budget != clientValues.Budget)
                    {
                        ModelState.AddModelError("Budget", "Current value: "
                                                 + String.Format("{0:c}", databaseValues.Budget));
                    }
                    if (databaseValues.StartDate != clientValues.StartDate)
                    {
                        ModelState.AddModelError("StartDate", "Current value: "
                                                 + String.Format("{0:d}", databaseValues.StartDate));
                    }
                    if (databaseValues.InstructorID != clientValues.InstructorID)
                    {
                        ModelState.AddModelError("InstructorID", "Current value: "
                                                 +
                                                 db.Instructors.Find(databaseValues.InstructorID).FullName);
                    }
                    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.RowVersion = databaseValues.RowVersion;
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                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, "ID", "FullName",
                                                  department.InstructorID);
            return(View(department));
        }