public ActionResult CourseAssign(Department aDepartment, Course aCourse, Teacher aTeacher)
        {
            PopulateDropdownList();
            if (aDepartment.DepartmentId == 0 || aCourse.CourseId == 0 || aTeacher.TeacherId == 0)
            {
                ViewBag.Message = "All fields are required";

                return View();
            }

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

            if (aCourse.Teacher != null)
            {
                PopulateDrodownList(aCourse, aTeacher);
                ViewBag.Message = "This Course already assigned to another teacher ";
                return View();

            }
            aCourse.Teacher = aTeacher;
            aCourse.Department = aDepartment;

            if (!ModelState.IsValid)
            {
                db.Entry(aCourse).State = EntityState.Modified;
                db.SaveChanges();
                aTeacher.RemainingCredit -= aCourse.Credit;
                db.Entry(aTeacher).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("ViewCourseStatus");
            }
            return View();
        }
 private void PopulateDrodownList(Course aCourse, Teacher aTeacher)
 {
     var teachers = db.Teachers.Where(t => t.DepartmentId == aTeacher.DepartmentId).ToList();
     var courses = db.Courses.Where(s => s.DepartmentId == aCourse.DepartmentId).ToList();
     ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code");
     ViewBag.TeacherId = new SelectList(teachers, "TeacherId", "Name", aTeacher.Name);
     ViewBag.CourseId = new SelectList(courses, "CourseId", "Code", aCourse.Code);
 }