public ActionResult Create( int courseId, CourseInstanceCreateEdit newCourseInstance ) { newCourseInstance.BaseCourse = db.Course.Where( c => c.ID == courseId ).FirstOrDefault(); if( newCourseInstance.Year < 0 || newCourseInstance.BaseCourse == null ) { return View( newCourseInstance ); } List<Person> studentsList = new List<Person>(); for( int i = 0; i < newCourseInstance.Chosen_Students.Count(); i++ ) { if( newCourseInstance.Chosen_Students[i] ) { string id = newCourseInstance.Students[i].Id; Person student = db.Users.Where( p => p.Id == id ).FirstOrDefault(); studentsList.Add( student ); } } var courseInstance = new CourseInstance { Year = newCourseInstance.Year, AdditionalDescription = newCourseInstance.AdditionalDescription, BaseCourse = newCourseInstance.BaseCourse, Place = newCourseInstance.Place, Report_Date = newCourseInstance.Report_Date, Students = studentsList, }; db.CourseInstance.Add( courseInstance ); db.SaveChanges(); return RedirectToAction( "course", "Courses", new { id = courseId } ); }
public ActionResult Edit( int courseId, int id, bool cameFromCoursePage, CourseInstanceCreateEdit editedCourseInstance ) { if( editedCourseInstance.Year < 0 || editedCourseInstance.BaseCourse == null ) { return View( editedCourseInstance ); } List<Person> studentsList = new List<Person>(); for( int i = 0; i < editedCourseInstance.Chosen_Students.Count(); i++ ) { if( editedCourseInstance.Chosen_Students[i] ) { string studentId = editedCourseInstance.Students[i].Id; Person student = db.Users.Where( p => p.Id == studentId ).FirstOrDefault(); studentsList.Add( student ); } } var courseInstance = db.CourseInstance.Where( c => c.ID == editedCourseInstance.ID ).First(); if( courseInstance == null ) { return HttpNotFound(); } courseInstance.Year = editedCourseInstance.Year; courseInstance.AdditionalDescription = editedCourseInstance.AdditionalDescription; courseInstance.BaseCourse = db.Course.Where( c => c.ID == editedCourseInstance.BaseCourse.ID ).First(); courseInstance.Place = editedCourseInstance.Place; courseInstance.Report_Date = editedCourseInstance.Report_Date; courseInstance.Students.Clear(); courseInstance.Students = studentsList; db.SaveChanges(); if( cameFromCoursePage ) { return RedirectToAction( "course", "Courses", new { id = courseId } ); } else { return RedirectToAction( "CourseInstance", new { id = id } ); } }