public async Task <ActionResult> Edit(MajorEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var major = await MajorManager.FindEntityByIdAsync(model.Id);

                if (major == null)
                {
                    ModelState.AddModelError("", "未找到此项major");
                    return(View(model));
                }
                var depart = await DepartmentManager.FindEntityByIdAsync(model.DepartmentId);

                if (depart == null)
                {
                    ModelState.AddModelError("", "未找到此项depart");
                    return(View(model));
                }
                major.Name       = model.MajorName;
                major.Department = depart;
                await MajorManager.UpdateEntityAsync(major);

                return(RedirectToAction("Index"));
            }
            return(View(model));
        }
        public async Task <ActionResult> Delete(int?id)
        {
            if (ModelState.IsValid)
            {
                var major = await MajorManager.FindEntityByIdAsync(id ?? 0);

                if (major != null)
                {
                    await MajorManager.DeleteEntityAsync(major);
                }
            }
            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> Edit(int?id)
        {
            var major = await MajorManager.FindEntityByIdAsync(id ?? 0);

            if (major == null)
            {
                return(HttpNotFound());
            }
            var departments = await DepartmentManager.GetAllEntities().ToListAsync();

            SelectList selectList = new SelectList(departments, "Id", "Name");

            ViewBag.selectList = selectList;
            var model = new MajorEditViewModel {
                Id = major.Id, DepartmentId = major.Department.Id, MajorName = major.Name
            };

            return(View(model));
        }
        public async Task <ActionResult> Edit(ClassEditViewModel model)
        {
            var departments = await DepartmentManager.GetAllEntities().ToListAsync();

            SelectList departSelectList = new SelectList(departments.ToList(), "Id", "Name");

            ViewBag.departSelectList = departSelectList;

            var majors = await MajorManager.GetMajorsForDepartment(departments.ElementAtOrDefault(0)).ToListAsync();

            SelectList majorSelectList = new SelectList(majors.ToList(), "Id", "Name");

            ViewBag.majorSelectList = majorSelectList;

            var grades = await GradeManager.GetAllEntities().ToListAsync();

            SelectList gradeSelectList = new SelectList(grades.ToList(), "Id", "GradeNum");

            ViewBag.gradeSelectList = gradeSelectList;

            var teachers = (await UserManager.GetNoClassTeachersAsync()).ToList();

            ViewBag.teacherSelectList = new SelectList(teachers, "Id", "Name");

            ViewBag.Classes = await ClassManager.GetAllEntities().ToListAsync();

            if (ModelState.IsValid)
            {
                var class1 = await ClassManager.FindEntityByIdAsync(model.Id);

                if (class1 == null)
                {
                    ModelState.AddModelError("", "找不到class1");
                    return(View(model));
                }
                var grade = await GradeManager.FindEntityByIdAsync(model.GradeId);

                if (grade == null)
                {
                    ModelState.AddModelError("", "找不到grade");
                    return(View(model));
                }
                var major = await MajorManager.FindEntityByIdAsync(model.MajorId);

                if (major == null)
                {
                    ModelState.AddModelError("", "找不到major");
                    return(View(model));
                }
                var teacher = await UserManager.FindByIdAsync(model.ClassTeacherId);

                if (teacher == null)
                {
                    ModelState.AddModelError("", "找不到teacher");
                    return(View(model));
                }
                class1.Major           = major;
                class1.Grade           = grade;
                class1.ClassDefination = model.Defination;
                class1.ClassTeacher    = teacher.TeacherInfo;

                await ClassManager.UpdateEntityAsync(class1);

                teacher.TeacherInfo.ManageClass = class1;
                await UserManager.UpdateAsync(teacher);

                await UserManager.RemoveStudentsFromClassAsync(model.IdsToRemove ?? new string[] { });

                return(RedirectToAction("Index"));
            }
            return(View(model));
        }
        public async Task <ActionResult> Create(ClassCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var departments = await DepartmentManager.GetAllEntities().ToListAsync();

                SelectList departSelectList = new SelectList(departments, "Id", "Name");
                ViewBag.departSelectList = departSelectList;

                var majors = await MajorManager.GetMajorsForDepartment(departments.ElementAtOrDefault(0)).ToListAsync();

                SelectList majorSelectList = new SelectList(majors, "Id", "Name");
                ViewBag.majorSelectList = majorSelectList;

                var grades = await GradeManager.GetAllEntities().ToListAsync();

                SelectList gradeSelectList = new SelectList(grades, "Id", "GradeNum");
                ViewBag.gradeSelectList = gradeSelectList;

                var teachers = (await UserManager.GetNoClassTeachersAsync()).ToList();
                ViewBag.teacherSelectList = new SelectList(teachers, "Id", "Name");

                ViewBag.Classes = await ClassManager.GetAllEntities().ToListAsync();

                var grade = await GradeManager.FindEntityByIdAsync(model.GradeId);

                if (grade == null)
                {
                    ModelState.AddModelError("", "找不到grade");
                    return(View(model));
                }
                var major = await MajorManager.FindEntityByIdAsync(model.MajorId);

                if (major == null)
                {
                    ModelState.AddModelError("", "找不到major");
                    return(View(model));
                }
                var teacher = await UserManager.FindByIdAsync(model.ClassTeacherId);

                if (teacher == null)
                {
                    ModelState.AddModelError("", "找不到teacher");
                    return(View(model));
                }
                var class1 = new Class
                {
                    ClassDefination = model.Defination,
                    Grade           = grade,
                    Major           = major,
                    ClassTeacher    = teacher.TeacherInfo
                };
                if (await ClassManager.IsEntityExistByLamdaAsync(c =>
                                                                 c.Grade.Id == class1.Grade.Id && c.Major.Id == class1.Major.Id &&
                                                                 c.ClassDefination == class1.ClassDefination))
                {
                    ModelState.AddModelError("", "已存在Class");
                    return(View(model));
                }
                var class2 = await ClassManager.AddEntityAsync(class1);

                teacher.TeacherInfo.ManageClass = class2;
                await UserManager.UpdateAsync(teacher);


                return(RedirectToAction("Index"));
            }
            return(View(model));
        }