private void LoadDropDownList(ClassRoomAllocation classRoomAllocation)
 {
     var courses = db.Courses.Where(s => s.DepartmentId == classRoomAllocation.DepartmentId);
     ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", classRoomAllocation.DepartmentId);
     ViewBag.CourseId = new SelectList(courses.ToArray(), "CourseId", "Code", classRoomAllocation.CourseId);
     ViewBag.ClassRoomId = new SelectList(db.ClassRooms, "ClassRoomId", "RoomNo", classRoomAllocation.ClassRoomId);
     ViewBag.DayId = new SelectList(db.Days, "DayId", "Name", classRoomAllocation.DayId);
 }
        public ActionResult Create(ClassRoomAllocation classRoomAllocation)
        {
            Create();
            if (classRoomAllocation.DepartmentId == 0 || classRoomAllocation.CourseId == 0 || classRoomAllocation.ClassRoomId == 0 || classRoomAllocation.DayId == 0)
            {
                return View();
            }
            string timeFrom = classRoomAllocation.TimeFrom;
            string timeTo = classRoomAllocation.TimeTo;

            Course aCourse = (db.Courses.Where(course => course.CourseId == classRoomAllocation.CourseId)).Single();
            Department aDepartment = (db.Departments.Where(d => d.DepartmentId == aCourse.DepartmentId)).Single();
            Day day = db.Days.Single(d => d.DayId == classRoomAllocation.DayId);
            ClassRoom classRoom = db.ClassRooms.Single(c => c.ClassRoomId == classRoomAllocation.ClassRoomId);
            List<ClassRoomAllocation> allocationRooms = (db.ClassRoomAllocations.Where(c =>
                (c.DayId == classRoomAllocation.DayId &&
                 c.ClassRoomId == classRoomAllocation.ClassRoomId))).ToList();

            List<ClassRoomAllocation> allocationCourses =
                db.ClassRoomAllocations.Where(
                    c => (c.DayId == classRoomAllocation.DayId && c.CourseId == classRoomAllocation.CourseId)).ToList();
            string timeFrom2 = "";
            string timeTo2 = "";
            int id=0;
            Boolean confirm = false;
            if (timeFrom == timeTo) confirm = true;

            foreach (ClassRoomAllocation room in allocationRooms)
            {
                bool check = CheckOverlapping(timeFrom, timeTo, room.TimeFrom, room.TimeTo);
                if (check)
                {
                    confirm = true;
                    timeFrom2 = room.TimeFrom;
                    timeTo2 = room.TimeTo;
                    id = room.CourseId;
                }
            }

            foreach (ClassRoomAllocation room in allocationCourses)
            {
                bool check = CheckOverlapping(timeFrom, timeTo, room.TimeFrom, room.TimeTo);
                if (check)
                {
                    confirm = true;
                }
            }

            if (!confirm)
            {
                LoadDropDownList(classRoomAllocation);
                classRoomAllocation.Department = aDepartment;
                if (ModelState.IsValid)
                {
                    db.ClassRoomAllocations.Add(classRoomAllocation);
                    db.SaveChanges();
                    if (aCourse.Teacher == null)
                    {
                        ViewBag.Success = aCourse.Code + " has been allocated in " + classRoom.RoomNo +
                                          " class room. And the schedual is " + day.Name + " at " +
                                          classRoomAllocation.TimeFrom + "-" + classRoomAllocation.TimeTo +
                                          " and teacher is not assigned yet.";
                    }
                    else
                    {
                        ViewBag.Success = aCourse.Code + " has been allocated in " + classRoom.RoomNo +
                                          " class room. And the schedual is " + day.Name + " at " +
                                          classRoomAllocation.TimeFrom + "-" + classRoomAllocation.TimeTo +
                                          " and the teacher of this course is" + aCourse.Teacher.Name;
                    }
                    return View();
                }
            }
            Course course2 = db.Courses.Single(c => c.CourseId == id);
            LoadDropDownList(classRoomAllocation);
            if (course2.Teacher == null)
            {
                ViewBag.ErrorMessage = "This Room: " + classRoom.RoomNo + " has already allocated at " + timeFrom2 + "-" + timeTo2 + " for " + course2.Code +
                                   " and teacher is not assigned yet.";
            }
            else
            {
                ViewBag.ErrorMessage = "This Room: " + classRoom.RoomNo + " has already allocated at " + timeFrom2 + "-" + timeTo2 + " for " + course2.Code +
                                   " and the teacher of this course is "+course2.Teacher.Name;
            }

            return View(classRoomAllocation);
        }
 public ActionResult Edit(ClassRoomAllocation classroomallocation)
 {
     if (ModelState.IsValid)
     {
         db.Entry(classroomallocation).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", classroomallocation.DepartmentId);
     ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Code", classroomallocation.CourseId);
     ViewBag.ClassRoomId = new SelectList(db.ClassRooms, "ClassRoomId", "RoomNo", classroomallocation.ClassRoomId);
     ViewBag.DayId = new SelectList(db.Days, "DayId", "Name", classroomallocation.DayId);
     return View(classroomallocation);
 }