public IActionResult SelectAjax([FromBody] List <Testt> a) { if (ModelState.IsValid) { Student student = _context.Students.Include(s => s.DisciplineStudents).FirstOrDefault(s => s.Name == User.Identity.Name); for (int i = 0; i < a.Count; i++) { Discipline discipline = _context.Disciplines .FirstOrDefault(c => c.Title == a[i].Title); var studDis = student.DisciplineStudents.FirstOrDefault(sd => sd.DisciplineId == discipline.Id); DisciplineStudent disciplineStudent = new DisciplineStudent() { DisciplineId = discipline.Id, StudentId = student.Id }; if (a[i].IsChecked == true) { if (!student.DisciplineStudents.Contains(studDis)) { student.DisciplineStudents.Add(disciplineStudent); } } else { if (student.DisciplineStudents.Contains(studDis)) { student.DisciplineStudents.Remove(studDis); } } _context.SaveChanges(); } Discipline.Clear(); var students = _context.Students.Include(c => c.DisciplineStudents).ThenInclude(sc => sc.Discipline).ToList(); SelectedDiscipline = students.FirstOrDefault(s => s.Id == student.Id).DisciplineStudents.Select(c => c.Discipline).ToList(); var disciplines = _context.Disciplines.ToList().Except(SelectedDiscipline).ToList(); NewDisceplines(SelectedDiscipline, true); NewDisceplines(disciplines, false); return(PartialView("~/Views/Home/SelectList.cshtml", Discipline)); } return(PartialView("~/Views/Home/SelectList.cshtml", Discipline)); }
public IActionResult Select(int?id) { Discipline.Clear(); if (id == null) { return(NotFound()); } var student = _context.Students .FirstOrDefault(m => m.Id == id); var students = _context.Students.Include(c => c.DisciplineStudents).ThenInclude(sc => sc.Discipline).ToList(); SelectedDiscipline = students.FirstOrDefault(s => s.Id == student.Id).DisciplineStudents.Select(c => c.Discipline).ToList(); var disciplines = _context.Disciplines.ToList().Except(SelectedDiscipline).ToList(); NewDisceplines(SelectedDiscipline, true); NewDisceplines(disciplines, false); SelectViewModel selectViewModel = new SelectViewModel(student, Discipline); return(View(selectViewModel)); }