public ActionResult Save(Student student) { if (!ModelState.IsValid) { var cohorts = _context.Cohorts.ToList(); var viewModel = new CohortsForSingleStudentViewModel { CohortTypes = cohorts, Student = student }; return(View("StudentForm", viewModel)); } if (student.Id == 0) { _context.Students.Add(student); } else { var studentInDb = _context.Students.Single(s => s.Id == student.Id); studentInDb.Name = student.Name; studentInDb.CohortTypeId = student.CohortTypeId; } _context.SaveChanges(); return(RedirectToAction("Index", "Student")); }
public ActionResult New() { var cohorts = _context.Cohorts.ToList(); var viewModel = new CohortsForSingleStudentViewModel { CohortTypes = cohorts, Student = new Student() }; return(View("StudentForm", viewModel)); }
public ActionResult Edit(int id) { var cohorts = _context.Cohorts.ToList(); var student = _context.Students.SingleOrDefault(s => s.Id == id); if (student == null) { return(HttpNotFound()); } var viewModel = new CohortsForSingleStudentViewModel { CohortTypes = cohorts, Student = student }; return(View("StudentForm", viewModel)); }