public ActionResult Create(Student aStudent, Course aCourse, DateTime date)
        {
            Create();
            if (aStudent.StudentId == 0 || aCourse.CourseId == 0)
            {
                ViewBag.ErrorMessage = "All fields are required.";
                return View();
            }

            Enrollment enrollment = new Enrollment();
            enrollment.StudentId = aStudent.StudentId;
            enrollment.CourseId = aCourse.CourseId;
            enrollment.Date = date;

            Student student = (db.Students.Where(s => s.StudentId == aStudent.StudentId)).Single();
            Course course = db.Courses.Single(c => c.CourseId == aCourse.CourseId);

            enrollment.Student = student;
            enrollment.Course = course;

            bool check = db.Enrollments.Count(c => c.StudentId == aStudent.StudentId && c.CourseId == aCourse.CourseId) ==0;
            if (check)
            {
                db.Enrollments.Add(enrollment);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ErrorMessage =  "This course has already enrolled for this student";
            return View(enrollment);
        }
        public ActionResult StudentResultEntry(Enrollment enrollment)
        {
            LoadDropdownList(enrollment);

            if (enrollment.StudentId == 0 || enrollment.CourseId == 0 || enrollment.GradeLetterId == null)
            {
                ViewBag.Message = "All fields are required.";
                return View();
            }
            GradeLetter gradeLetter = (db.GradeLetters.Where(g => g.GradeLetterId == enrollment.GradeLetterId)).Single();
            Course course = (db.Courses.Where(c => c.CourseId == enrollment.CourseId)).Single();
            Student student = (db.Students.Where(s => s.StudentId == enrollment.StudentId)).Single();
            Enrollment enrolled = (db.Enrollments.Where(e => (e.StudentId == student.StudentId && e.CourseId == course.CourseId))).Single();

            bool check = db.Enrollments.Count(e => (e.StudentId == student.StudentId && e.CourseId == course.CourseId && e.GradeLetter != null)) == 1;
            enrollment = enrolled;
            enrollment.GradeLetter = gradeLetter;
            enrollment.GradeLetterId = gradeLetter.GradeLetterId;
            enrollment.Student = enrolled.Student;
            enrollment.StudentId = enrolled.StudentId;
            enrollment.Course = course;
            if (!check)
            {
                db.Entry(enrollment).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("ViewResult");
            }

               ViewBag.Message = "This course Result already assigned for this Student ";
            return View(enrollment);
        }
 private void LoadDropdownList(Enrollment enrollment)
 {
     List<Course> enrolledCourses = GetEnrolledCourses(enrollment.StudentId);
     ViewBag.StudentId = new SelectList(db.Students, "StudentId", "RegNo");
     ViewBag.CourseId = new SelectList(enrolledCourses, "CourseId", "Code");
     ViewBag.GradeLetterId = new SelectList(db.GradeLetters, "GradeLetterId", "Name");
 }
 public ActionResult SelectStudent(int? studentId)
 {
     Student student = (db.Students.Where(s => s.StudentId == studentId)).Single();
     Enrollment enrollment = new Enrollment();
     enrollment.Student = student;
     return PartialView("~/Views/Shared/_StudentDetails.cshtml", enrollment);
 }