public async Task <bool> UpdateStudentModulesMarks(CourseStudentMarksViewModel model) { var marksIds = model.Marks.Select(x => x.CourseModuleId); var marksToUpdate = await _context.Mark.Include(x => x.CourseModule).Where(x => marksIds.Contains(x.CourseModuleId)).ToListAsync(); model.Marks.ForEach(x => { var toUpdate = marksToUpdate.First(y => y.CourseModule.ModuleId == x.ModuleId); toUpdate.StudentId = x.StudentId; toUpdate.LabMark = x.LabMark; toUpdate.TestMark = x.TestMark; toUpdate.Student = _context.Student.FirstOrDefault(z => z.StudentId == x.StudentId); _context.Mark.Update(toUpdate); }); return(await _context.SaveChangesAsync() >= 0); }
public async Task <IActionResult> SetStudentMark(CourseStudentMarksViewModel model) { if (ModelState.IsValid) { try { var result = await _coursesRepository.UpdateStudentModulesMarks(model); if (result) { // success return(RedirectToAction("Details", "Courses", new { courseId = model.Course.CourseId })); } } catch { // Error return(RedirectToAction("Index", "Courses")); } } // Error for Model (Model.AddError or smth like that) return(View(model)); }
public IActionResult SetStudentMark(int courseId, int studentId) { CourseStudentMarksViewModel model = _coursesRepository.GetStudentModulesMarks(courseId, studentId); return(View(model)); }