public PartialViewResult DeleteCourse(int courseID, string userName) { using (ChalkitDbContext db = new ChalkitDbContext()) { try { Course existingCourse = db.Courses.Find(courseID); if (existingCourse != null) { db.Courses.Remove(existingCourse); } db.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } } ViewBag.AuthorName = userName; return PartialView("_AuthorCourseUpdateCreate", new Course()); }
public ActionResult CreateOrModifyCourse(string userName, Course course) { Author userAuthor = new Author(); using (ChalkitDbContext db = new ChalkitDbContext()) { try { userAuthor = db.Authors.Find(userName); Course existingCourse = db.Courses.Find(course.CourseID); if (existingCourse == null) { course.Exercises = new List<Exercise>(); db.Courses.Add(course); db.Entry(userAuthor).Collection(x => x.Courses).Load(); userAuthor.Courses.Add(course); } else { db.Entry(existingCourse).Collection(x => x.Exercises).Load(); existingCourse.CourseName = course.CourseName; existingCourse.CourseDescription = course.CourseDescription; } db.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } } return RedirectToAction("Index", "Author"); }