Exemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, StudentsInCourseVM viewmodel)
        {
            if (id != viewmodel.Course.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(viewmodel.Course);
                    await _context.SaveChangesAsync();

                    IEnumerable <int>       listStudents = viewmodel.SelectedStudents;
                    IQueryable <Enrollment> toBeRemoved  = _context.Enrollment.Where
                                                               (s => !listStudents.Contains(s.StudentId) && s.CourseId == id);
                    _context.Enrollment.RemoveRange(toBeRemoved);

                    IEnumerable <int> existStudents = _context.Enrollment
                                                      .Where(s => listStudents.Contains(s.StudentId) && s.CourseId == id).Select(s => s.StudentId);
                    IEnumerable <int> newStudents = listStudents.Where(s => !existStudents.Contains(s));
                    foreach (int studentID in newStudents)
                    {
                        _context.Enrollment.Add(new Enrollment {
                            StudentId = studentID, CourseId = id
                        });
                    }
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CourseExists(viewmodel.Course.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["FirstTeacherID"]  = new SelectList(_context.Set <Teacher>(), "Id", "FullName", viewmodel.Course.FirstTeacherID);
            ViewData["SecondTeacherID"] = new SelectList(_context.Set <Teacher>(), "Id", "FullName", viewmodel.Course.SecondTeacherID);
            return(View(viewmodel));
        }
Exemplo n.º 2
0
        // GET: Courses/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var course = _context.Course.Where(m => m.Id == id).Include(m => m.Students).First();

            if (course == null)
            {
                return(NotFound());
            }
            StudentsInCourseVM viewmodel = new StudentsInCourseVM
            {
                Course           = course,
                StudentList      = new MultiSelectList(_context.Student.OrderBy(s => s.StudentId), "Id", "FullName"),
                SelectedStudents = course.Students.Select(sa => sa.StudentId)
            };

            ViewData["FirstTeacherID"]  = new SelectList(_context.Set <Teacher>(), "Id", "FullName", course.FirstTeacherID);
            ViewData["SecondTeacherID"] = new SelectList(_context.Set <Teacher>(), "Id", "FullName", course.SecondTeacherID);
            return(View(viewmodel));
        }