public ActionResult EditCourse(Course edit) { if (ModelState.IsValid) { if (IsCourseExisted(edit, true)) { //if (CourseCanBeUpdated(edit)) //{ Course course = db.Courses.SingleOrDefault(c => c.courseId == edit.courseId); db.Entry(course).CurrentValues.SetValues(edit); db.SaveChanges(); return RedirectToAction("ViewAllCourse"); //} //else // ModelState.AddModelError("Status", "This course has annual course !"); } else ModelState.AddModelError("courseName", "This course name have been existed !"); } ViewBag.state = new SelectList(getListStatus(), "Value", "Text", edit.Status); ViewBag.roles = new SelectList(db.Faculties, "facultyId", "facultyName", edit.facultyId); return View(edit); }
public bool IsCourseExisted(Course manipulateCourse, bool edit) { if (edit == false)//--> Add Course { var course = db.Courses.SingleOrDefault(c => c.courseName == manipulateCourse.courseName && c.facultyId == manipulateCourse.facultyId); if (course == null)//--> there is no course have same name with addCourse(can be Added to DB) return true; } else { var course = db.Courses.SingleOrDefault(c => c.courseName == manipulateCourse.courseName && c.facultyId == manipulateCourse.facultyId && c.courseId != manipulateCourse.courseId); if (course == null)//--> there is no course have same name with addCourse(can be Added to DB) return true; } return false; }
public ActionResult AddCourse(Course c) { if (ModelState.IsValid) { //c.courseName = c.courseName + " - " + getFacultyNameFromFacultyId(c.facultyId); if (IsCourseExisted(c, false)) { db.sp_InsertCourse(c.courseName, c.facultyId); db.SaveChanges(); return RedirectToAction("ViewAllCourse"); } else ModelState.AddModelError("courseName", "This course name have been existed !"); } ViewBag.roles = new SelectList(db.Faculties, "facultyId", "facultyName", c.facultyId); return View(c); }