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, "DepartmentId", "Code", assigncourse.DepartmentId);
     ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "Name", assigncourse.TeacherId);
     ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Code", assigncourse.CourseId);
     return View(assigncourse);
 }
        public ActionResult Create(AssignCourse assigncourse)
        {
            if (ModelState.IsValid)
            {
                var result =
                    db.AssignCourses.Count(
                        r => r.CourseId == assigncourse.CourseId ) == 0;

                if (result)
                {
                    Teacher aTeacher = db.Teachers.FirstOrDefault(t => t.TeacherId == assigncourse.TeacherId);
                    Course aCourse = db.Courses.FirstOrDefault(c => c.CourseId == assigncourse.CourseId);
                    List<AssignCourse> assignTeachers =
                        db.AssignCourses.Where(t => t.TeacherId == assigncourse.TeacherId).ToList();
                    AssignCourse assign = null;
                    if (assignTeachers.Count != 0)
                    {

                        assign = assignTeachers.Last();
                        assigncourse.RemaingCredit = assign.RemaingCredit;
                    }
                    else
                    {
                        assigncourse.RemaingCredit = aTeacher.CreditTaken;
                    }

                    //if (assign == null)
                    //    assigncourse.RemaingCredit = aTeacher.CreditTaken;

                    if (assigncourse.RemaingCredit < aCourse.Credit)
                    {
                        Session["Teacher"] = aTeacher;
                        Session["Course"] = aCourse;
                        Session["AssignedCourse"] = assigncourse;
                        Session["AssigneddCourseCheck"] = assign;
                        return RedirectToAction("AskToAssign");
                    }

                    assigncourse.CreditTaken = aTeacher.CreditTaken;

                    if (assign == null)
                    {
                        assigncourse.RemaingCredit = aTeacher.CreditTaken - aCourse.Credit;
                    }
                    else
                    {
                        assigncourse.RemaingCredit = assign.RemaingCredit - aCourse.Credit;
                    }

                    aCourse.AssignTo = aTeacher.Name;

                    db.AssignCourses.Add(assigncourse);
                    db.SaveChanges();
                    TempData["success"] = "Course Assigned";
                    return RedirectToAction("Create");
                }
                else
                {
                    TempData["Already"] = "Course Has Already Been Assigned";
                    return RedirectToAction("Create");
                }
            }

            ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", assigncourse.DepartmentId);
            ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "Name", assigncourse.TeacherId);
            ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Code", assigncourse.CourseId);
            return View(assigncourse);
        }