public ActionResult Create(AssignCourse assigncourse)
        {
            if (ModelState.IsValid)
            {
                var alreadyAssigned = db.AssignCourses.Any(a => a.CourseId == assigncourse.CourseId);
                if (alreadyAssigned)
                {
                    var teacherName =
                        db.AssignCourses.Include(a => a.Teacher)
                            .Where(a => a.CourseId == assigncourse.CourseId)
                            .Select(a => a.Teacher.Name);

                    var teacher = "";

                    foreach (var t in teacherName)
                    {
                        teacher = t;
                    }

                    TempData["error"] = "The Course is already Assigned to " + teacher;
                }
                else
                {
                    int td = 0, cd = 0;
                    var teacherDept = db.Teachers.Where(a => a.Id == assigncourse.TeacherId).Select(a => a.DepartmentId);
                    foreach (var i in teacherDept)
                    {
                        td = i;
                    }
                    var courseDept = db.Courses.Where(a => a.Id == assigncourse.CourseId).Select(a => a.DepartmentId);
                    foreach (var i in courseDept)
                    {
                        cd = i;
                    }

                    if (td == cd)
                    {
                        db.AssignCourses.Add(assigncourse);
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        TempData["error"] = "This course not belongs to teachers department(vice versa)";
                    }
                }

            }

            ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Code", assigncourse.DepartmentId);
            ViewBag.TeacherId = new SelectList(db.Teachers, "Id", "Name", assigncourse.TeacherId);
            ViewBag.CourseId = new SelectList(db.Courses, "Id", "Code", assigncourse.CourseId);
            return View(assigncourse);
        }
 public ActionResult Edit(AssignCourse assigncourse)
 {
     if (ModelState.IsValid)
     {
         db.Entry(assigncourse).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Code", assigncourse.DepartmentId);
     ViewBag.TeacherId = new SelectList(db.Teachers, "Id", "Name", assigncourse.TeacherId);
     ViewBag.CourseId = new SelectList(db.Courses, "Id", "Code", assigncourse.CourseId);
     return View(assigncourse);
 }