Пример #1
0
        public async Task <IActionResult> StudentCourseEditPost(int?id, string[] selectedCourses)
        {
            if (id == null)//method called with form which accepts hidden ID
            {
                return(NotFound());
            }
            //get provided user (from id)
            Student student = await _context.Students
                              .Include(i => i.Enrollments)
                              .ThenInclude(i => i.Course)
                              .SingleOrDefaultAsync(m => m.Id == id);

            //comparing user and person who calls this action
            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            if (student.Id != user.Id)
            {
                return(RedirectToAction("AccessDenied", "Account"));
            }
            if (selectedCourses != null)
            {
                if (ModelState.IsValid)
                {
                    //creat empty list for entities
                    student.Enrollments = new List <Enrollment>();
                    var selectedCoursesHS  = new HashSet <string>(selectedCourses);
                    var studentEnrollments = new HashSet <int>
                                                 (student.Enrollments.Select(c => c.Course.CourseID));
                    //loop to add/remove courses
                    foreach (var course in _context.Courses)
                    {
                        if (selectedCoursesHS.Contains(course.CourseID.ToString()))
                        {
                            if (!studentEnrollments.Contains(course.CourseID))
                            {
                                student.Enrollments.Add(new Enrollment {
                                    SmID = student.Id, CourseID = course.CourseID
                                });
                            }
                        }
                        else
                        {
                            if (studentEnrollments.Contains(course.CourseID))
                            {
                                Enrollment courseToRemove = student.Enrollments.SingleOrDefault(i => i.CourseID == course.CourseID);
                                _context.Remove(courseToRemove);
                            }
                        }
                    }
                    var result = await TryUpdateModelAsync <Student>(student, "", di => di.Enrollments);

                    try
                    {
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateException /* ex */)
                    {
                        //Log the error (uncomment ex variable name and write a log.)
                        ModelState.AddModelError("", "Unable to save changes. " +
                                                 "Try again, and if the problem persists, " +
                                                 "see your system administrator.");
                    }
                }
            }
            return(RedirectToAction("StudentIndex"));
        }
        public ActionResult Navigation(int id)
        {
            Enrollment enroll = db.Enrollments.Find(id);

            return(View(enroll));
        }