public ActionResult Edit(EditInstructorViewModel model, string[] selectedCourses)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Instructor instructor = db.Instructors
                                            .Include(i => i.OfficeAssignment)
                                            .Include(i => i.Courses)
                                            .Single(i => i.ID == model.ID);

                    instructor.LastName         = model.LastName;
                    instructor.FirstMidName     = model.FirstMidName;
                    instructor.HireDate         = model.HireDate;
                    instructor.OfficeAssignment = string.IsNullOrWhiteSpace(model.OfficeAssignment.Location) ? null : model.OfficeAssignment;

                    UpdateInstructorCourses(selectedCourses, instructor);

                    db.Entry(instructor).State = EntityState.Modified;
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
            }

            PopulateAssignedCourseData(model);

            return(View(model));
        }
        // GET: Instructor/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Instructor instructor = db.Instructors
                                    .Include(i => i.OfficeAssignment)
                                    .Include(i => i.Courses)
                                    .Single(i => i.ID == id);

            if (instructor == null)
            {
                return(HttpNotFound());
            }

            EditInstructorViewModel model = new EditInstructorViewModel
            {
                ID               = instructor.ID,
                LastName         = instructor.LastName,
                FirstMidName     = instructor.FirstMidName,
                HireDate         = instructor.HireDate,
                Courses          = instructor.Courses,
                OfficeAssignment = instructor.OfficeAssignment
            };

            PopulateAssignedCourseData(model);

            return(View(model));
        }
Example #3
0
        public IActionResult Create()
        {
            var courses = _courseRepository.GetAll();

            var model = new EditInstructorViewModel()
            {
                AssignedCourseData = courses.Select(s => new AssignedCourseData()
                {
                    CourseId   = s.CourseId,
                    CourseName = s.CourseName,
                    Assigned   = false
                }).ToList()
            };

            return(View(model));
        }
        private void PopulateAssignedCourseData(EditInstructorViewModel model = null)
        {
            var allCourses        = db.Courses;
            var instructorCourses = model == null ? null : new HashSet <int>(model.Courses.Select(c => c.CourseID));
            var viewModel         = new List <AssignedCourseData>();

            foreach (var course in allCourses)
            {
                viewModel.Add(new AssignedCourseData
                {
                    CourseID = course.CourseID,
                    Title    = course.Title,
                    Assigned = instructorCourses == null ? false : instructorCourses.Contains(course.CourseID)
                });
            }

            ViewBag.Courses = viewModel;
        }