public ActionResult CreateSubject() { SubjectsCreateVM model = new SubjectsCreateVM(); model.Courses = unitOfWork.CourseRepository.GetAll().ToList(); return View("CreateEditSubject", model); }
public ActionResult CreateEditSubject(SubjectsCreateVM model) { if (ModelState.IsValid) { Subject subject; if (model.Id == 0) { subject = new Subject(); } else { subject = unitOfWork.SubjectRepository.GetById(model.Id); } subject.Name = model.Name; if (model.Id == 0) { unitOfWork.SubjectRepository.Insert(subject); TempData.FlashMessage("Subject has been created!"); } else { unitOfWork.SubjectRepository.Update(subject); TempData.FlashMessage("Subject has been edited!"); } unitOfWork.Save(); model.Courses = unitOfWork.CourseRepository.GetAll().ToList(); List<Course> courses = new List<Course>(); foreach (var s in model.Courses) { if ((Request.Form[s.Id.ToString()] != null) && (Request.Form[s.Id.ToString()] == "on")) { courses.Add(s); } } unitOfWork.CoursesSubjectsRepository.UpdateTable(subject, courses); return RedirectToAction("ManageSubjects", "Admin"); } model.Courses = unitOfWork.CourseRepository.GetAll().ToList(); return View(model); }
public ActionResult EditSubject(int? id) { if (!id.HasValue) { return RedirectToAction("ManageSubjects", "Admin"); } Subject subject = unitOfWork.SubjectRepository.GetById(id.Value); if (subject == null) { return RedirectToAction("ManageSubjects", "Admin"); } SubjectsCreateVM model = new SubjectsCreateVM(); model.Id = subject.Id; model.Name = subject.Name; model.Courses = unitOfWork.CourseRepository.GetAll().ToList(); return View("CreateEditSubject", model); }