public ActionResult Create(EnrollCourse model) { if (ModelState.IsValid) { db.EnrollCourses.Add(model); db.SaveChanges(); return RedirectToAction("Index"); } return View(model); }
public ActionResult Create(EnrollCourse enrollcourse) { if (ModelState.IsValid) { var ret = db.EnrollCourses.Any( s => (s.StudentId == enrollcourse.StudentId && s.CourseId == enrollcourse.CourseId)); if (ret) { TempData["error"] = "Course Already Enrolled to this student"; return RedirectToAction("Create"); } else { int sd = 0, cd = 0; var studentDept = db.Students.Where(a => a.Id == enrollcourse.StudentId).Select(a => a.DepartmentId); foreach (var i in studentDept) { sd = i; } var courseDept = db.Courses.Where(a => a.Id == enrollcourse.CourseId).Select(a => a.DepartmentId); foreach (var i in courseDept) { cd = i; } if (sd == cd) { db.EnrollCourses.Add(enrollcourse); db.SaveChanges(); return RedirectToAction("Index"); } else { TempData["error"] = "This course not belongs to student department(vice versa)"; } } } ViewBag.StudentId = new SelectList(db.Students, "Id", "RegistrationId", enrollcourse.StudentId); ViewBag.CourseId = new SelectList(db.Courses, "Id", "Code", enrollcourse.CourseId); return View(enrollcourse); }
public ActionResult Create(EnrollCourse enrollcourse) { if (ModelState.IsValid) { var result = db.EnrollCourses.Count(u => u.StudentId == enrollcourse.StudentId && u.CourseId == enrollcourse.CourseId) == 0; if (result) { TempData["success"] = "Course Enrolled"; db.EnrollCourses.Add(enrollcourse); db.SaveChanges(); return RedirectToAction("Create"); } else { TempData["Already"] = "Student Has Already Enrolled This Course"; return RedirectToAction("Create"); } } ViewBag.StudentId = new SelectList(db.Students, "StudentId", "RegistrationId", enrollcourse.StudentId); ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Code", enrollcourse.CourseId); return View(enrollcourse); }
public ActionResult Edit(EnrollCourse enrollcourse) { if (ModelState.IsValid) { db.Entry(enrollcourse).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.StudentId = new SelectList(db.Students, "StudentId", "RegistrationId", enrollcourse.StudentId); ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Code", enrollcourse.CourseId); return View(enrollcourse); }