public ActionResult NewCourse(int Id = 0) { List <CoordinatorDetails> coordinators = new List <CoordinatorDetails>(); EditCourseItem viewModel = new EditCourseItem(); using (EastWoodEntities db = new EastWoodEntities()) { coordinators = db.Coordinators .Join( db.Users, c => c.UserId, u => u.UserId, (c, u) => new CoordinatorDetails { UserId = u.UserId, UserBio = u.UserBio, UserContactNo = u.UserContactNo, UserEmail = u.UserEmail, UserFirstName = u.UserFirstName, UserLastLoggedIn = u.UserLastLoggedIn, UserLastName = u.UserLastName, UserPassword = u.UserPassword, CoordinatorId = c.CoordinatorId, RoleId = u.RoleId } ).ToList(); if (Id != 0) { viewModel = db.Courses .Where(w => w.CourseId == Id) .Join( db.Coordinators, course => course.CoordinatorId, coordinator => coordinator.CoordinatorId, (course, coordinator) => new EditCourseItem { CourseName = course.CourseName, CourseId = course.CourseId, CourseDescription = course.CourseDescription, CourseEndsOn = course.CourseEndsOn, CourseStartsOn = course.CourseStartsOn, CoordinatorId = course.CoordinatorId }) .FirstOrDefault(); } } viewModel.Coordinators = coordinators; // New if (Id == 0) { return(View(viewModel)); } // Edit else { return(View(viewModel)); } }
public ActionResult NewCourse(EditCourseItem CourseItem) { Course course = new Course(); using (EastWoodEntities db = new EastWoodEntities()) { if (CourseItem.CourseId != 0) { course = db.Courses.Where(w => w.CourseId == CourseItem.CourseId).FirstOrDefault(); } course.CourseName = CourseItem.CourseName; course.CourseDescription = CourseItem.CourseDescription; course.CourseEndsOn = CourseItem.CourseEndsOn; course.CourseStartsOn = CourseItem.CourseStartsOn; course.CoordinatorId = CourseItem.CoordinatorId; if (CourseItem.CourseId == 0) { db.Courses.Add(course); } db.SaveChanges(); } return(Redirect("/Admin/Courses")); }