コード例 #1
0
        public ActionResult Edit(DegreeProgram degreeprogram, IEnumerable <RequiredCourse> RequiredCourses, IEnumerable <ElectiveCourse> ElectiveCourses)
        {
            if (ModelState.IsValid)
            {
                DegreeProgram degreeAttached = Request <DegreeProgram> .GetItemByID(degreeprogram.ID, "A", "B");

                degreeprogram.requiredCourses = degreeAttached.requiredCourses;
                degreeprogram.electiveCourses = degreeAttached.electiveCourses;

                if (RequiredCourses == null)
                {
                    RequiredCourses = new List <RequiredCourse>();
                }

                if (ElectiveCourses == null)
                {
                    ElectiveCourses = new List <ElectiveCourse>();
                }

                //figures out which courses were deleted from the form
                LinkedList <RequiredCourse> toRemove = new LinkedList <RequiredCourse>();
                foreach (RequiredCourse course in degreeAttached.requiredCourses)
                {
                    if (course.ID > 0)
                    {
                        if (!RequiredCourses.Contains(course))
                        {
                            RequiredCourse reqcourseAttached = Request <RequiredCourse> .GetItemByID(course.ID, "A", "B");

                            toRemove.AddFirst(reqcourseAttached);
                        }
                    }
                }
                //deletes those courses from the list
                while (toRemove.Count > 0)
                {
                    RequiredCourse removeme = toRemove.First();
                    toRemove.RemoveFirst();
                    Request <RequiredCourse> .Delete(removeme.ID, "A", "B");
                }
                //clears the list
                degreeprogram.requiredCourses = new List <RequiredCourse>();
                //adds the courses that exist to the list
                foreach (RequiredCourse reqcourse in RequiredCourses)
                {
                    RequiredCourse reqcourseAttached = null;;
                    if (reqcourse.ID > 0)
                    {
                        reqcourseAttached = Request <RequiredCourse> .GetItemByID(reqcourse.ID, "A", "B");

                        Request <RequiredCourse> .Update(reqcourseAttached, reqcourse, "A", "B");
                    }
                    else
                    {
                        if (Request <Course> .GetItemByID(reqcourse.courseID, "A", "B") != null)
                        {
                            int id = Request <RequiredCourse> .Add(reqcourse, "A", "B");

                            reqcourseAttached = Request <RequiredCourse> .GetItemByID(id, "A", "B");
                        }
                    }
                    if (reqcourseAttached != null)
                    {
                        degreeprogram.requiredCourses.Add(reqcourseAttached);
                    }
                }

                //figures out which elective lists were deleted from the list
                LinkedList <ElectiveCourse> toRemoveMe = new LinkedList <ElectiveCourse>();
                foreach (ElectiveCourse course in degreeAttached.electiveCourses)
                {
                    if (!ElectiveCourses.Contains(course))
                    {
                        ElectiveCourse elcourseAttached = Request <ElectiveCourse> .GetItemByID(course.ID, "A", "B");

                        toRemoveMe.AddFirst(elcourseAttached);
                    }
                }
                //remove those elective lists from the list
                while (toRemoveMe.Count > 0)
                {
                    ElectiveCourse removeme = toRemoveMe.First();
                    toRemoveMe.RemoveFirst();
                    Request <ElectiveCourse> .Delete(removeme.ID, "A", "B");
                }
                //clear the list
                degreeprogram.electiveCourses = new List <ElectiveCourse>();
                //add the existing elective lists to the list
                foreach (ElectiveCourse elcourse in ElectiveCourses)
                {
                    ElectiveCourse elcourseAttached = null;;
                    if (elcourse.ID > 0)
                    {
                        elcourseAttached = Request <ElectiveCourse> .GetItemByID(elcourse.ID, "A", "B");

                        Request <ElectiveCourse> .Update(elcourseAttached, elcourse, "A", "B");
                    }
                    else
                    {
                        if (Request <ElectiveList> .GetItemByID(elcourse.electiveListID, "A", "B") != null)
                        {
                            int id = Request <ElectiveCourse> .Add(elcourse, "A", "B");

                            elcourseAttached = Request <ElectiveCourse> .GetItemByID(id, "A", "B");
                        }
                    }
                    if (elcourseAttached != null)
                    {
                        degreeprogram.electiveCourses.Add(elcourseAttached);
                    }
                }

                Request <DegreeProgram> .Update(degreeAttached, degreeprogram, "A", "B");

                return(RedirectToAction("Index"));
            }
            if (RequiredCourses != null)
            {
                foreach (RequiredCourse course in RequiredCourses)
                {
                    if (course.courseID > 0)
                    {
                        course.course = Request <Course> .GetItemByID(course.courseID, "A", "B");
                    }
                }
            }
            degreeprogram.requiredCourses = RequiredCourses.ToList();

            if (ElectiveCourses != null)
            {
                foreach (ElectiveCourse course in ElectiveCourses)
                {
                    if (course.electiveListID > 0)
                    {
                        course.electiveList = Request <ElectiveList> .GetItemByID(course.electiveListID, "A", "B");
                    }
                }
            }
            degreeprogram.electiveCourses = ElectiveCourses.ToList();

            return(View(degreeprogram));
        }
コード例 #2
0
        public ActionResult Edit(DegreeProgram degreeprogram, IEnumerable <RequiredCourse> RequiredCourses, IEnumerable <ElectiveCourse> ElectiveCourses)
        {
            if (ModelState.IsValid)
            {
                //DegreeProgram degreeAttached = degreePrograms.Where(degree => degree.ID == degreeprogram.ID).First();
                DegreeProgram degreeAttached = _degreeProgramProducer.Get(new DegreeProgram()
                {
                    ID = degreeprogram.ID
                });
                degreeprogram.requiredCourses = degreeAttached.requiredCourses;
                degreeprogram.electiveCourses = degreeAttached.electiveCourses;

                if (RequiredCourses == null)
                {
                    RequiredCourses = new List <RequiredCourse>();
                }

                if (ElectiveCourses == null)
                {
                    ElectiveCourses = new List <ElectiveCourse>();
                }

                //figures out which courses were deleted from the form
                LinkedList <RequiredCourse> toRemove = new LinkedList <RequiredCourse>();
                foreach (RequiredCourse course in degreeAttached.requiredCourses)
                {
                    if (course.ID > 0)
                    {
                        if (!RequiredCourses.Contains(course))
                        {
                            //RequiredCourse reqcourseAttached = requiredCourses.Where(reqc => reqc.ID == course.ID).First();
                            RequiredCourse reqcourseAttached = _requiredCourseProducer.Get(new RequiredCourse()
                            {
                                ID = course.ID
                            });
                            toRemove.AddFirst(reqcourseAttached);
                        }
                    }
                }
                //deletes those courses from the list
                while (toRemove.Count > 0)
                {
                    RequiredCourse removeme = toRemove.First();
                    toRemove.RemoveFirst();
                    //requiredCourses.Remove(removeme);
                    _requiredCourseProducer.Remove(removeme);
                }
                //clears the list
                degreeprogram.requiredCourses.Clear();
                //adds the courses that exist to the list
                foreach (RequiredCourse reqcourse in RequiredCourses)
                {
                    RequiredCourse reqcourseAttached = null;;
                    if (reqcourse.ID > 0)
                    {
                        //reqcourseAttached = requiredCourses.Where(reqc => reqc.ID == reqcourse.ID).First();
                        reqcourseAttached = _requiredCourseProducer.Get(new RequiredCourse()
                        {
                            ID = reqcourse.ID
                        });
                        //requiredCourses.UpdateValues(reqcourseAttached, reqcourse);
                        _requiredCourseProducer.Update(reqcourse);
                    }
                    else
                    {
                        //if (courses.Find(reqcourse.courseID) != null)
                        if (_courseProducer.Get(new Course()
                        {
                            ID = reqcourse.courseID
                        }) != null)
                        {
                            //requiredCourses.Add(reqcourse);
                            //requiredCourses.SaveChanges();
                            _requiredCourseProducer.Create(reqcourse);
                            //reqcourseAttached = requiredCourses.Where(reqc => reqc.ID == reqcourse.ID).First();
                            reqcourseAttached = _requiredCourseProducer.Get(new RequiredCourse()
                            {
                                ID = reqcourse.ID
                            });
                        }
                    }
                    if (reqcourseAttached != null)
                    {
                        degreeprogram.requiredCourses.Add(reqcourseAttached);
                    }
                }

                //figures out which elective lists were deleted from the list
                LinkedList <ElectiveCourse> toRemoveMe = new LinkedList <ElectiveCourse>();
                foreach (ElectiveCourse course in degreeAttached.electiveCourses)
                {
                    if (!ElectiveCourses.Contains(course))
                    {
                        //ElectiveCourse elcourseAttached = electiveCourses.Where(elc => elc.ID == course.ID).First();
                        ElectiveCourse elcourseAttached = _electiveCourseProducer.Get(new ElectiveCourse()
                        {
                            ID = course.ID
                        });
                        toRemoveMe.AddFirst(elcourseAttached);
                    }
                }
                //remove those elective lists from the list
                while (toRemoveMe.Count > 0)
                {
                    ElectiveCourse removeme = toRemoveMe.First();
                    toRemoveMe.RemoveFirst();
                    //electiveCourses.Remove(removeme);
                    _electiveCourseProducer.Remove(removeme);
                }
                //clear the list
                degreeprogram.electiveCourses.Clear();
                //add the existing elective lists to the list
                foreach (ElectiveCourse elcourse in ElectiveCourses)
                {
                    ElectiveCourse elcourseAttached = null;;
                    if (elcourse.ID > 0)
                    {
                        //elcourseAttached = electiveCourses.Where(elc => elc.ID == elcourse.ID).First();
                        elcourseAttached = _electiveCourseProducer.Get(new ElectiveCourse()
                        {
                            ID = elcourse.ID
                        });
                        //electiveCourses.UpdateValues(elcourseAttached, elcourse);
                        _electiveCourseProducer.Update(elcourse);
                    }
                    else
                    {
                        //if (electiveLists.Find(elcourse.electiveListID) != null)
                        if (_electiveListProducer.Get(new ElectiveList()
                        {
                            ID = elcourse.electiveListID
                        }) != null)
                        {
                            //electiveCourses.Add(elcourse);
                            //electiveCourses.SaveChanges();
                            _electiveCourseProducer.Create(elcourse);
                            //elcourseAttached = electiveCourses.Where(elc => elc.ID == elcourse.ID).First();
                            elcourseAttached = _electiveCourseProducer.Get(new ElectiveCourse()
                            {
                                ID = elcourse.ID
                            });
                        }
                    }
                    if (elcourseAttached != null)
                    {
                        degreeprogram.electiveCourses.Add(elcourseAttached);
                    }
                }

                //degreePrograms.UpdateValues(degreeAttached, degreeprogram);
                //degreePrograms.SaveChanges();
                _degreeProgramProducer.Update(degreeprogram);
                return(RedirectToAction("Index"));
            }
            if (RequiredCourses != null)
            {
                foreach (RequiredCourse course in RequiredCourses)
                {
                    if (course.courseID > 0)
                    {
                        //course.course = courses.Find(course.courseID);
                        course.course = _courseProducer.Get(new Course()
                        {
                            ID = course.courseID
                        });
                    }
                }
            }
            degreeprogram.requiredCourses = RequiredCourses.ToList();

            if (ElectiveCourses != null)
            {
                foreach (ElectiveCourse course in ElectiveCourses)
                {
                    if (course.electiveListID > 0)
                    {
                        //course.electiveList = electiveLists.Find(course.electiveListID);
                        course.electiveList = _electiveListProducer.Get(new ElectiveList()
                        {
                            ID = course.electiveListID
                        });
                    }
                }
            }
            degreeprogram.electiveCourses = ElectiveCourses.ToList();

            return(View(degreeprogram));
        }