public ActionResult EditTeacher(TeacherEditTeacherVM model) { if (!ModelState.IsValid) { model.TitleList = TeacherTitle(); return View(model); } UnitOfWork unitOfWork = new UnitOfWork(); UserRepository<Teacher> teacherRepo = new UserRepository<Teacher>(unitOfWork); TitleRepository tiRepo = new TitleRepository(unitOfWork); Teacher teacher = teacherRepo.GetByID(model.TeacherID); try { int TitleId = Convert.ToInt32(model.selectedValueID); Title title = tiRepo.GetByID(TitleId); if (model.FirstName != teacher.FirstName || model.LastName != teacher.LastName || model.Active != teacher.Active || TitleId != teacher.Title.ID) { teacher.FirstName = model.FirstName; teacher.LastName = model.LastName; teacher.Username = model.FirstName; teacher.Active = model.Active; teacher.Title = title; teacherRepo.Save(teacher); unitOfWork.Commit(); } } catch (Exception) { unitOfWork.RollBack(); } return RedirectToAction("ListTeachers", "Teacher"); }
public ActionResult CreateTeacher(TeacherCreateTeacherVM model) { UnitOfWork unitOfWork = new UnitOfWork(); Teacher teacher = new Teacher(); UserRepository<Teacher> teacherRepo = new UserRepository<Teacher>(unitOfWork); TitleRepository titleRepo = new TitleRepository(unitOfWork); if (!ModelState.IsValid) { model.TitleList = TeacherTitle(); return View(model); } try { teacher.Username = model.FirstName; teacher.FirstName = model.FirstName; teacher.LastName = model.LastName; int tittleId = Convert.ToInt32(model.selectedValueID); Title title = titleRepo.GetByID(tittleId); teacher.Title = title; UniversitySystem.Hasher.Passphrase hash = UniversitySystem.Hasher.PasswordHasher.Hash("password"); teacher.Hash = hash.Hash; teacher.Salt = hash.Salt; teacher.Active = true; teacherRepo.Save(teacher); unitOfWork.Commit(); } catch (Exception) { unitOfWork.RollBack(); throw; } return RedirectToAction("ListTeachers", "Teacher"); }
public ActionResult EditTeacher(int id) { TeacherEditTeacherVM model = new TeacherEditTeacherVM(); TitleRepository titleRepo = new TitleRepository(); List<SelectListItem> list = new List<SelectListItem>(); UserRepository<Teacher> teacherRepo = new UserRepository<Teacher>(); Teacher teacher = teacherRepo.GetByID(id); var teacherTitle = titleRepo.GetByID(teacher.Title.ID); list.Add(new SelectListItem() { Text = teacherTitle.Name, Value = teacherTitle.ID.ToString() }); var title = titleRepo.GetAll(); foreach (var item in title) { if (item.ID != teacher.Title.ID) { list.Add(new SelectListItem() { Text = item.Name, Value = item.ID.ToString() }); } } model.FirstName = teacher.FirstName; model.LastName = teacher.LastName; model.Active = teacher.Active; model.TeacherID = teacher.ID; model.TitleList = list; return View(model); }