public IActionResult AttachDetachGetView(int studentId) { var students = _studentService.GetStudents(); var groups = _groupService.GetGroups(); var currentStudent = students.FirstOrDefault(x => x.Id == studentId); if (currentStudent == null) { throw new ArgumentNullException("studentId not exists."); } var attachDetachStudentToGroup = new AttachDetachStudentToGroup { StudentId = currentStudent.Id }; ViewBag.StudentList = new SelectList(students.Select(s => new { Text = $"{s.FirstName}{s.LastName}", Value = s.Id, Selected = s.Id }), "Value", "Text"); ViewBag.GroupsList = new SelectList(groups.Select(g => new { Text = g.Name, Value = g.Id, Selected = g.Id }), "Value", "Text"); return(View("AttachStudentToGroup", attachDetachStudentToGroup)); }
public StudentVm AttachStudentToGroup(AttachDetachStudentToGroup attachDetachStudentToGroup) { if (attachDetachStudentToGroup == null) { throw new Exception("attach is null"); } var student = _dbContext.Users.OfType <Student>().FirstOrDefault(x => x.Id == attachDetachStudentToGroup.StudentId); if (student == null) { throw new Exception("student is null"); } var group = _dbContext.Groups.FirstOrDefault(x => x.Id == attachDetachStudentToGroup.GroupId); if (group == null) { throw new Exception("group is null"); } student.GroupId = group.Id; _dbContext.SaveChanges(); var studentVm = Mapper.Map <StudentVm>(student); return(studentVm); }
public IActionResult AttachStudentToGroup(AttachDetachStudentToGroup attachDetachStudentToGroupDto) { if (ModelState.IsValid) { _groupService.AttachStudentToGroup(attachDetachStudentToGroupDto); return(RedirectToAction("Index")); } return(View()); }