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);
            bool check = db.Enrollments.Count(c => c.StudentId == aStudent.StudentId && c.CourseId == aCourse.CourseId) == 0;
            if (check)
            {
                db.Enrollments.Add(enrollment);
                db.SaveChanges();
                ViewBag.Message = course.Code + " has assigned for " + student.RegNo;
                return View(enrollment);
            }

            enrollment.Student = student;
            enrollment.Course = course;
            ViewBag.ErrorMessage = student.RegNo+" has already enrolled for "+course.Code;
            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);
 }
        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("Index");
                ViewBag.Saved = student.Name+" got "+gradeLetter.Name+" in "+course.Name;
                return View(enrollment);
            }

            ViewBag.Message = course.Name + " course grade already assigned for "+student.Name;
            return View(enrollment);
        }
 public ActionResult Edit(Enrollment enrollment)
 {
     if (ModelState.IsValid)
     {
         db.Entry(enrollment).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.StudentId = new SelectList(db.Students, "StudentId", "RegNo", enrollment.StudentId);
     ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Code", enrollment.CourseId);
     return View(enrollment);
 }