コード例 #1
0
        public object Edit([Bind(Prefix = "")] Course course, string id, string ProfessorList)
        {
            ViewData["Title"] = "Edit Course";
            ViewData["Message"] = "Edit Course Details!";
            var cp = new CourseProvider();
            int courseId;
            try
            {
                courseId = Convert.ToInt32(id);
            }
            catch (Exception)   //not an integer
            {
                return RedirectToAction("Index", "Course");
            }

            course = cp.GetCourseByID(courseId);
            // redisplay form immediately if there are input format errors
            if (!ModelState.IsValid)
            {
                ModelState.CopyValidationExceptions();
                PrepareProfessorList(course);
                return View("Edit", course);
            }
            try {
                UpdateModel(course, new[] { "CourseName", "Monday", "Tuesday", "Wednesday",
                                            "Thursday", "Friday", "Saturday", "Sunday",
                                            "StartHour", "StartMinute", "EndHour", "EndMinute"});
                if (!ProfessorList.Equals("0")) {
                    Professor prof = new ProfessorProvider().GetProfessorByID(Convert.ToInt32(ProfessorList));
                    course.AssignedProfessor = prof;
                } else {
                    course.AssignedProfessor = null;
                }
                cp.UpdateCourse(course);
                return View("Saved");
            } catch (RuleViolationException vex) {
                ViewData.ModelState.CopyValidationExceptions(vex);
                PrepareProfessorList(course);
                return View(course);
            }
            catch (Exception err) {
                ViewData.ModelState.CopyValidationExceptions(err, "course");
                PrepareProfessorList(course);
                return View(course);
            }
        }