public ActionResult Create(Enrollment enrollment, Course aCourse)
        {
            // validation check
            string message = ValidationForEnrollment(enrollment, aCourse);
            if (message != "")
            {
                ViewBag.StudentId = new SelectList(db.Students, "StudentId", "StudentRegistrationNo");
                ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseName");
                ViewBag.Message = message;

                return View();
            }

            // assign course & student to the Enrollment
            aCourse = (from c in db.Courses where c.CourseId == aCourse.CourseId select c).SingleOrDefault();
            Student aStudent =
                (from s in db.Students where s.StudentId == enrollment.StudentId select s).SingleOrDefault();

            enrollment.Course = aCourse;
            enrollment.Student = aStudent;
               // enrollment.Course.CourseId = aCourse.CourseId;

            if (!ModelState.IsValid)
            {
                db.Enrollments.Add(enrollment);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.StudentId = new SelectList(db.Students, "StudentId", "StudentRegistrationNo", enrollment.StudentId);
            ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseName", enrollment.Course.CourseId);

            return View();
        }
        public ActionResult CourseAssign(Department aDepartment, Course aCourse, Teacher aTeacher)
        {
            string message = ValidationForCourseAssign(aDepartment, aCourse, aTeacher);
            if (message != "")
            {
                ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName");
                ViewBag.TeacherId = new SelectList("", "", "TeacherName");
                ViewBag.CourseId = new SelectList("", "", "CourseName");

                ViewBag.Message = message;
                return View();
            }

            aTeacher = db.Teachers.Find(aTeacher.TeacherId);
            aCourse = db.Courses.Find(aCourse.CourseId);

            // assiging teacher to this course
            aCourse.Teacher = aTeacher;

            // update remaining credit for teacher
            aTeacher.TeacherRemainingCredit -= aCourse.CourseCredit;

            //save data to database
            if (!ModelState.IsValid)
            {
                db.Entry(aCourse).State = EntityState.Modified;
                db.SaveChanges();

                db.Entry(aTeacher).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            return View();
        }
        private string ValidationForResultEntry(Enrollment enrollment, Course aCourse)
        {
            string message = "";
            if (aCourse.CourseId < 1 || enrollment.StudentId < 1 || enrollment.GradeId < 1 || aCourse == null || enrollment.GradeId == null || enrollment.StudentId == null)
            {
                message += "Fill Up all fields.";
                return message;
            }
            else
            {
                try
                {
                    Student aStudent = (from s in db.Students where s.StudentId == enrollment.StudentId select s).SingleOrDefault();
                    aCourse = (from c in db.Courses where c.CourseId == aCourse.CourseId select c).SingleOrDefault();

                    Enrollment aEnrollment =
                        (from e in db.Enrollments
                         where e.StudentId == aStudent.StudentId && e.Course.CourseId == aCourse.CourseId
                         select e).SingleOrDefault();
                    if (aEnrollment == null)
                    {
                        message += "Student is not enrolled in this course.";
                    }
                    else
                    {
                        if (aEnrollment.GradeId > 0)
                        {
                            message = "Grade is already added to this course";
                        }
                    }
                }
                catch (Exception)
                {
                    message += " ";
                }

            }
            return message;
        }
        private string ValidationForEnrollment(Enrollment enrollment, Course aCourse)
        {
            string message = "";
            if (aCourse == null || aCourse.CourseId == 0 || enrollment.StudentId == 0 || enrollment.StudentId == null)
            {
                message += "Please fill up all the options";
            }
            else
            {

                // what about student not found, course not found
                // case not considered due to fake trust

                aCourse = (from c in db.Courses where c.CourseId == aCourse.CourseId select c).SingleOrDefault();
                List<Enrollment> enrollments =
                    (from e in db.Enrollments where e.StudentId == enrollment.StudentId select e).ToList();

                //check student is already enrolled in this course or not
                bool alreadyEnrollerd = false;
                foreach (Enrollment e in enrollments.Where(e => e.Course.CourseId == aCourse.CourseId))
                {
                    alreadyEnrollerd = true;
                }

                if (alreadyEnrollerd)
                {
                    message += "Student Already Enrolled in this Course";
                }
            }
            return message;
        }
        public ActionResult ResultEntry(Enrollment enrollment, Course aCourse)
        {
            string message = ValidationForResultEntry(enrollment, aCourse);
            if (message != "")
            {
                ViewBag.StudentId = new SelectList(db.Students, "StudentId", "StudentRegistrationNo");
                ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseName");
                ViewBag.GradeId = new SelectList(db.Grades, "GradeId", "GradeCode");

                ViewBag.Message = message;
                return View();
            }

            Grade aGrade = (from g in db.Grades where g.GradeId == enrollment.GradeId select g).SingleOrDefault();
            Student aStudent = (from s in db.Students where s.StudentId == enrollment.StudentId select s).SingleOrDefault();
            aCourse = (from c in db.Courses where c.CourseId == aCourse.CourseId select c).SingleOrDefault();

            Enrollment aEnrollment =
                (from e in db.Enrollments
                 where e.StudentId == aStudent.StudentId && e.Course.CourseId == aCourse.CourseId
                 select e).SingleOrDefault();

            enrollment = aEnrollment;
            enrollment.Grade = aGrade;
            enrollment.GradeId = aGrade.GradeId;
            enrollment.Student = aStudent;
            enrollment.StudentId = aStudent.StudentId;
            enrollment.Course = aCourse;

            if (!ModelState.IsValid)
            {
                db.Entry(enrollment).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.StudentId = new SelectList(db.Students, "StudentId", "StudentRegistrationNo");
            ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseName");
            ViewBag.GradeId = new SelectList(db.Grades, "GradeId", "GradeCode");

            return View();
        }
        public ActionResult Create(Course course)
        {
            //check validation
            string message = ValidationForCourse(course);
            if (message != "")
            {
                ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentCode", course.DepartmentId);
                ViewBag.SemesterId = new SelectList(db.Semesters, "SemesterId", "SemesterName", course.SemesterId);
                ViewBag.Message = message;
                return View(course);
            }

            //save to database
            if (ModelState.IsValid)
            {
                db.Courses.Add(course);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentCode", course.DepartmentId);
            ViewBag.SemesterId = new SelectList(db.Semesters, "SemesterId", "SemesterName", course.SemesterId);
            return View(course);
        }
        private string ValidationForCourseAssign(Department aDepartment, Course aCourse, Teacher aTeacher)
        {
            string message = "";

            if (aDepartment.DepartmentId == 0 || aCourse.CourseId == 0 || aTeacher.TeacherId == 0)
            {
                message += "Incomplete form submit, any field is not yet set ";
            }

            // is teacher and course is not from the same department

            aDepartment = db.Departments.Find(aDepartment.DepartmentId);
            aTeacher = db.Teachers.Find(aTeacher.TeacherId);
            aCourse = db.Courses.Find(aCourse.CourseId);

            if (aDepartment.DepartmentId != aTeacher.DepartmentId || aCourse.DepartmentId != aTeacher.DepartmentId)
            {
                ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName");
                ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "TeacherName");
                ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseName");

                if (aDepartment.DepartmentId != aCourse.DepartmentId)
                {
                    message += "Your Course is not from the selected department";

                }
                else if (aCourse.DepartmentId != aTeacher.DepartmentId)
                {
                    message += "Your Teacher is not from the selected department";
                }

            }

            // if course is already assigned

            if (aCourse.Teacher != null)
            {
                message += "Course Already Assigned";
            }

            return message;
        }
        private string ValidationForCourse(Course course)
        {
            string message = "";
            if (course.CourseCode == "")
            {
                message += "Code not given";
            }

            if (course.CourseCode != "" && db.Courses.Count(c => c.CourseCode == course.CourseCode) != 0)
            {
                message += "Code already exists ";
            }
            if (course.CourseCredit < 0)
            {
                message += "Credit must be non-negative number";
            }
            if (course.CourseName == "")
            {
                message += "Name not given";
            }
            if (course.CourseName != "" && db.Courses.Count(c => c.CourseName == course.CourseName) != 0)
            {
                message += "Name already exists";
            }

            if (course.CourseDescription == "")
            {
                message += "Description not given";
            }

            if (course.DepartmentId < 1)
            {
                message += "Invalid Department Selected";
            }

            if (course.SemesterId < 1)
            {
                message += "Invalid Semester Selected";
            }

            return message;
        }