public IActionResult Update(long Id) { //get the course to update var course = _schoolDbContext .Courses .FirstOrDefault(c => c.Id == Id); //declare viewModel CoursesAddUpdateCourseViewModel coursesAddUpdateCourseViewModel = new CoursesAddUpdateCourseViewModel(); coursesAddUpdateCourseViewModel .Title = course?.Title; coursesAddUpdateCourseViewModel .TeacherId = course?.TeacherId; coursesAddUpdateCourseViewModel .CourseId = course.Id; coursesAddUpdateCourseViewModel .Teachers = new List <SelectListItem>(); foreach (var teacher in _schoolDbContext.Teachers.ToList()) { coursesAddUpdateCourseViewModel .Teachers.Add(new SelectListItem { Text = $"{teacher.Firstname} {teacher.Lastname}" , Value = $"{teacher.Id}" }); } return(View(coursesAddUpdateCourseViewModel)); }
public IActionResult Update(CoursesAddUpdateCourseViewModel coursesAddUpdateCourseViewModel) { if (!ModelState.IsValid) { //fill the teachers coursesAddUpdateCourseViewModel .Teachers = new List <SelectListItem>(); foreach (var teacher in _schoolDbContext.Teachers.ToList()) { coursesAddUpdateCourseViewModel .Teachers.Add(new SelectListItem { Text = $"{teacher.Firstname} {teacher.Lastname}" , Value = $"{teacher.Id}" }); } return(View(coursesAddUpdateCourseViewModel)); } //update course var course = _schoolDbContext .Courses .FirstOrDefault(c => c.Id == coursesAddUpdateCourseViewModel.CourseId); course.Title = coursesAddUpdateCourseViewModel .Title; course.TeacherId = coursesAddUpdateCourseViewModel .TeacherId; try { _schoolDbContext.SaveChanges(); } catch (SqlException e) { Console.WriteLine(e.Message); } //redirect to course list return(RedirectToAction("Index", "Courses")); }
public IActionResult AddCourse(CoursesAddUpdateCourseViewModel coursesAddCourseViewModel) { if (!ModelState.IsValid) { coursesAddCourseViewModel.Teachers = new List <SelectListItem>(); //loop over teachers foreach (var teacher in _schoolDbContext.Teachers.ToList()) { //add teachers to list coursesAddCourseViewModel.Teachers .Add(new SelectListItem { Text = $"{teacher.Firstname} {teacher.Lastname}" , Value = $"{teacher.Id}" }); } return(View(coursesAddCourseViewModel)); } //save new course Course newCourse = new Course(); newCourse.Title = coursesAddCourseViewModel.Title; //teacher newCourse.TeacherId = coursesAddCourseViewModel.TeacherId; _schoolDbContext.Courses.Add(newCourse); try { _schoolDbContext.SaveChanges(); } catch (SqlException e) { Console.WriteLine(e.Message); } //redirect to index return(RedirectToAction("Index", "Courses")); }
public IActionResult AddCourse() { //loads the form //viewModel CoursesAddUpdateCourseViewModel coursesAddCourseViewModel = new CoursesAddUpdateCourseViewModel(); coursesAddCourseViewModel.Teachers = new List <SelectListItem>(); //loop over teachers foreach (var teacher in _schoolDbContext.Teachers.ToList()) { //add teachers to list coursesAddCourseViewModel.Teachers .Add(new SelectListItem { Text = $"{teacher.Firstname} {teacher.Lastname}" , Value = $"{teacher.Id}" }); } return(View(coursesAddCourseViewModel)); }