public ActionResult DeleteSubject(int? id) { if (!id.HasValue) { return RedirectToAction("ManageSubjects", "Admin"); } Subject subject = unitOfWork.SubjectRepository.GetById(id.Value); if (subject == null) { return RedirectToAction("ManageSubjects", "Admin"); } SubjectsDeleteVM model = new SubjectsDeleteVM(); model.Id = subject.Id; model.Name = subject.Name; return View(model); }
public ActionResult DeleteSubject(SubjectsDeleteVM model) { if (ModelState.IsValid) { Subject subject = unitOfWork.SubjectRepository.GetById(model.Id, true); List<CoursesSubjects> css = unitOfWork.CoursesSubjectsRepository.GetBySubjectId(subject.Id, true); if (css.Count > 0) { model.Name = subject.Name; TempData.FlashMessage("Subject cannot be deleted! It is assigned to one or more courses!", null, FlashMessageTypeEnum.Red); return View(model); } unitOfWork.GradeRepository.DeleteRange(subject.Grades.ToList()); subject.Grades.Clear(); unitOfWork.SubjectRepository.Delete(subject.Id); unitOfWork.Save(); TempData.FlashMessage("Subject has been deleted"); return RedirectToAction("ManageSubjects", "Admin"); } return View(model); }