public ActionResult AssignToCourse(int id) { TeacherRepository teacherRepo = new TeacherRepository(); Teacher teacher = teacherRepo.GetById(id); AdminControllerTeacherVM teacherModel = new AdminControllerTeacherVM(); CourseRepository courseRepo = new CourseRepository(); List<Course> courses = new List<Course>(); teacherModel.FirstName = teacher.FirstName; teacherModel.LastName = teacher.LastName; teacherModel.Id = id; courses = courseRepo.GetAll().OrderBy(c => c.Name).ToList(); List<SelectListItem> SelectList = new List<SelectListItem>(); SelectListItem select = null; teacherModel.CourseSubjectList = teacher.CourseSubject.ToList(); foreach (var item in courses) { select = new SelectListItem() { Text = item.Name, Value = item.Id.ToString() }; SelectList.Add(select); } teacherModel.ListItems = SelectList; return View(teacherModel); }
public ActionResult AssignToCourse(AdminControllerTeacherVM model, string[] checkedSubjects) { UnitOfWork unitOfWork = new UnitOfWork(); TeacherRepository teacherRepository = new TeacherRepository(unitOfWork); CourseSubjectRepository courseSubjectRepo = new CourseSubjectRepository(unitOfWork); TryUpdateModel(model); Teacher teacher = teacherRepository.GetById(model.Id); CourseSubject courseSubject = null; try { List<CourseSubject> courseSubjectsList = courseSubjectRepo.GetAll(c => c.CourseID == model.CourseID && c.Teacher.Any(t => t.Id == model.Id)); foreach (var item in courseSubjectsList) { teacher.CourseSubject.Remove(item); } if (checkedSubjects != null) { foreach (var item in checkedSubjects) { courseSubject = courseSubjectRepo.GetAll(filter: cs => cs.CourseID == model.CourseID && cs.SubjectID.ToString() == item).FirstOrDefault(); teacher.CourseSubject.Add(courseSubject); } } teacherRepository.Save(teacher); unitOfWork.Commit(); } catch (Exception) { unitOfWork.RollBack(); } return RedirectToAction("ManageTeachers", "Admin"); }
public ActionResult ManageTeachers() { TeacherRepository teacherRepository = new TeacherRepository(); AdminControllerTeacherVM teacherModel = new AdminControllerTeacherVM(); List<CourseSubject> courseSubject = new List<CourseSubject>(); CourseSubjectRepository courseSubjectRepo = new CourseSubjectRepository(); teacherModel.teacherList = teacherRepository.GetAll(); return View(teacherModel); }
public ActionResult EditTeachers(int id, AdminControllerTeacherVM teacherModel) { UnitOfWork uOw = new UnitOfWork(); Teacher teacher = null; Title title = new Title(); TitleRepository titleRepository = new TitleRepository(uOw); TeacherRepository teacherRepository = new TeacherRepository(uOw); TryUpdateModel(teacherModel); if (teacherModel.ListItems == null) { teacherModel.TitleList = titleRepository.GetAll(); List<SelectListItem> SelectListTitle = new List<SelectListItem>(); foreach (var item in teacherModel.TitleList) { SelectListTitle.Add(new SelectListItem() { Text = item.Name, Value = item.Id.ToString() }); } teacherModel.ListItems = SelectListTitle; } if (ModelState.IsValid) { if (id > 0) { teacher = teacherRepository.GetById(id); teacher.FirstName = teacherModel.FirstName; teacher.LastName = teacherModel.LastName; teacher.UserName = teacherModel.UserName; teacher.Password = teacherModel.Password; title = titleRepository.GetById(teacherModel.TitleID); teacher.Title = title; teacher.IsActive = teacherModel.isActive; teacherRepository.Save(teacher); uOw.Commit(); return RedirectToAction("ManageTeachers"); } else { teacher = teacherRepository.GetAll(filter: t => t.UserName == teacherModel.UserName).FirstOrDefault(); if (teacher == null) { teacher = new Teacher(); teacher.FirstName = teacherModel.FirstName; teacher.LastName = teacherModel.LastName; teacher.UserName = teacherModel.UserName; teacher.Password = SecurityService.CreateHash(teacherModel.Password); title = titleRepository.GetById(teacherModel.TitleID); teacher.Title = title; teacher.IsActive = teacherModel.isActive; teacherRepository.Save(teacher); uOw.Commit(); return RedirectToAction("ManageTeachers"); } else { throw new ArgumentException("Invalid username !"); } } } return View(teacherModel); }
public ActionResult EditTeachers(int id) { TeacherRepository teacherRepository = new TeacherRepository(); TitleRepository titleRepository = new TitleRepository(); Teacher teacher = new Teacher(); AdminControllerTeacherVM teacherModel = new AdminControllerTeacherVM(); List<SelectListItem> SelectListTitle = new List<SelectListItem>(); SelectListItem select = null; Title title = new Title(); teacher.Title = title; if (id > 0) { teacher = teacherRepository.GetById(id); select = new SelectListItem() { Text = teacher.Title.Name, Value = teacher.Title.Id.ToString() }; SelectListTitle.Add(select); } teacherModel.TitleList = titleRepository.GetAll(); foreach (var item in teacherModel.TitleList) { if (item.Id != teacher.Title.Id) { select = new SelectListItem() { Text = item.Name, Value = item.Id.ToString() }; SelectListTitle.Add(select); } } if (id > 0) { teacher = teacherRepository.GetById(id); teacherModel.FirstName = teacher.FirstName; teacherModel.LastName = teacher.LastName; teacherModel.UserName = teacher.UserName; teacherModel.Password = teacher.Password; teacherModel.isActive = teacher.IsActive; teacherModel.Id = id; } if (id == 0) { teacher.FirstName = teacherModel.FirstName; teacher.LastName = teacherModel.LastName; teacher.UserName = teacherModel.UserName; teacher.Password = teacherModel.Password; teacher.IsActive = teacherModel.isActive; } teacherModel.ListItems = SelectListTitle; return View(teacherModel); }
public ActionResult ShowTeachingSubjects(int id) { Teacher teacher = new Teacher(); TeacherRepository teacherRepository = new TeacherRepository(); teacher = teacherRepository.GetById(id); AdminControllerTeacherVM model = new AdminControllerTeacherVM(); model.FirstName = teacher.FirstName + " " + teacher.LastName; model.CourseSubjectList = teacher.CourseSubject.ToList(); return View(model); }