public async Task <IActionResult> EditPost(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var studentToUpdate = await _studentRepo.Get(id.Value).SingleOrDefaultAsync();

            if (await _modelBindingHelperAdaptor.TryUpdateModelAsync <Student>(this, studentToUpdate, "", s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
            {
                try
                {
                    studentToUpdate.ModifiedDate = DateTime.UtcNow;
                    await _studentRepo.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateException)
                {
                    ModelState.AddModelError("", "Unable to save changes.  Try again, and if the problem persists, see your system administrator");
                }
            }

            return(View(studentToUpdate));
        }
Example #2
0
        public async Task <IActionResult> EditPost(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var courseToUpdate = await _courseRepo.GetAll().SingleOrDefaultAsync(c => c.ID == id);

            if (courseToUpdate == null)
            {
                return(NotFound());
            }

            if (await _modelBindingHelperAdaptor.TryUpdateModelAsync <Course>(this, courseToUpdate, "", c => c.Credits, c => c.DepartmentID, c => c.Title))
            {
                try
                {
                    courseToUpdate.ModifiedDate = DateTime.UtcNow;
                    await _courseRepo.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateException)
                {
                    ModelState.AddModelError("", "Unable to save changes.  Try again, and if the problem persists, see your system administrator");
                }
            }

            PopulateDepartmentsDropDownList(courseToUpdate.DepartmentID);
            return(View(courseToUpdate));
        }
Example #3
0
        public async Task <IActionResult> Edit(int?id, string[] selectedCourses)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var instructorToUpdate = await _instructorRepo.GetAll()
                                     .Include(i => i.OfficeAssignment)
                                     .Include(i => i.CourseAssignments).ThenInclude(i => i.Course)
                                     .SingleOrDefaultAsync(s => s.ID == id);

            if (await _modelBindingHelperAdaptor.TryUpdateModelAsync <Instructor>(this, instructorToUpdate, "", i => i.FirstMidName, i => i.LastName, i => i.HireDate, i => i.OfficeAssignment))
            {
                if (String.IsNullOrWhiteSpace(instructorToUpdate.OfficeAssignment?.Location))
                {
                    instructorToUpdate.OfficeAssignment = null;
                }

                UpdateInstructorCourses(selectedCourses, instructorToUpdate);
                try
                {
                    instructorToUpdate.ModifiedDate = DateTime.UtcNow;
                    await _instructorRepo.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateException)
                {
                    ModelState.AddModelError("", "Unable to save changes.  Try again, and if the problem persists, see your system administrator.");
                }
            }

            UpdateInstructorCourses(selectedCourses, instructorToUpdate);
            PopulateAssignedCourseData(instructorToUpdate);
            return(View(instructorToUpdate));
        }