public ActionResult EditTeacher(ListTeacherViewModel model) { // Edita al profe y lo persiste en la base de datos try { if (ModelState.IsValid) { using (Models.DBContainer db = new Models.DBContainer()) { Teacher teacher = db.Teachers.Find(model.id_teacher); if (teacher == null) { TempData["Alert"] = "An error occurred, try again later"; return(RedirectToAction("ListTeachers")); } teacher.first_name = model.first_name; teacher.last_name = model.last_name; teacher.active = model.active; db.Entry(teacher).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("ListTeachers", "Admin")); } return(View(model)); } catch (Exception exc) { throw new Exception(exc.Message); } }
public ActionResult EditSubject(ListSubjectViewModelAdmin model) { // Modifica la materia y la persiste en la base de datos try { if (ModelState.IsValid) { using (Models.DBContainer db = new Models.DBContainer()) { Subject subject = db.Subjects.Find(model.id_subject); if (subject == null) { TempData["Alert"] = "An error occurred, try again later"; return(RedirectToAction("ListSubjects")); } subject.name = model.name; subject.desc = model.description; subject.capacity = model.capacity; subject.time_from = model.time_from; subject.time_to = model.time_to; subject.id_teacher = model.id_teacher; if (subject.time_to <= subject.time_from) { // Horario no válido ViewData["Error"] = "Ending time must be strictly greater than starting time"; PopulateDropDownList(); return(View(model)); } db.Entry(subject).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("ListSubjects", "Admin")); } // Si el modelo no es válido, tengo que volver a la misma pantalla, con // lo cual hay que rellenar de nuevo el DropDownList PopulateDropDownList(); return(View(model)); } catch (Exception exc) { throw new Exception(exc.Message); } }