//更新 Instructor 實體的 Courses 導覽屬性
        private void UpdateInstructorCourses(string[] selectedCourses, Instructor instructorToUpdate)
        {
            if (selectedCourses == null)
            {
                instructorToUpdate.CourseAssignments = new List <CourseAssignment>();
                return;
            }

            var selectedCoursesHS = new HashSet <string>(selectedCourses);
            var instructorCourses = new HashSet <int>
                                        (instructorToUpdate.CourseAssignments.Select(c => c.Course.CourseID));

            foreach (var course in _context.Courses)
            {
                if (selectedCoursesHS.Contains(course.CourseID.ToString()))
                {
                    if (!instructorCourses.Contains(course.CourseID))
                    {
                        instructorToUpdate.CourseAssignments.Add(new CourseAssignment {
                            InstructorID = instructorToUpdate.ID, CourseID = course.CourseID
                        });
                    }
                }
                else
                {
                    if (instructorCourses.Contains(course.CourseID))
                    {
                        CourseAssignment courseToRemove = instructorToUpdate.CourseAssignments.FirstOrDefault(i => i.CourseID == course.CourseID);
                        _context.Remove(courseToRemove);
                    }
                }
            }
        }
Exemple #2
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            var genre = await _context.Genres.FindAsync(id);

            _context.Remove(genre);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }