public async Task <IActionResult> AddAbsence(int id, AddAbsenceViewModel model) { var currentUser = _context.Users.FirstOrDefault(u => u.UserName == User.Identity.Name); if (id != model.StudentClassroomId) { return(NotFound()); } var studentClassroom = _context.StudentClassrooms .Include(s => s.Absences) .SingleOrDefault(s => s.Id == id); if (studentClassroom == null) { return(NotFound()); } var absence = new Absence() { Description = model.Description, AbsenceDate = model.AbsenceDate, StudentClassroomId = model.StudentClassroomId, Student = currentUser, LostLessons = new List <Lesson>() }; if (ModelState.IsValid) { try { studentClassroom.Absences.Add(absence); _context.Update(studentClassroom); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StudentClassroomExists(studentClassroom.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(model)); }
//GET: FrequencyAndMarks/Addabsence/5 public async Task <IActionResult> AddAbsence(int?id) { var studentClassroom = await _context.StudentClassrooms.SingleOrDefaultAsync(m => m.Id == id); if (studentClassroom == null) { return(NotFound()); } var model = new AddAbsenceViewModel() { StudentClassroomId = (int)id }; return(View(model)); }