public async Task <ActionResult> EditClass(ClassEditViewModel model)
        {
            var departments = await InstitutionManager.GetAllDepartmentsAsync();

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

            ViewBag.departSelectList = departSelectList;

            var majors = await InstitutionManager.GetMajorsForDepartmentAsync(departments.ElementAtOrDefault(0));

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

            ViewBag.majorSelectList = majorSelectList;

            var grades = await InstitutionManager.GetAllGradesAsync();

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

            ViewBag.gradeSelectList = gradeSelectList;

            ViewBag.Classes = await InstitutionManager.GetAllClassesAsync();

            if (ModelState.IsValid)
            {
                var class1 = await InstitutionManager.FindClassByIdAsync(model.Id);

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

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

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

                await InstitutionManager.UpdateClassAsync(class1);

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

                return(RedirectToAction("AllClass"));
            }
            return(View(model));
        }
        public async Task <ActionResult> CreateClass(ClassCreateViewModel model)
        {
            var departments = await InstitutionManager.GetAllDepartmentsAsync();

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

            ViewBag.departSelectList = departSelectList;

            var majors = await InstitutionManager.GetMajorsForDepartmentAsync(departments.ElementAtOrDefault(0));

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

            ViewBag.majorSelectList = majorSelectList;

            var grades = await InstitutionManager.GetAllGradesAsync();

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

            ViewBag.gradeSelectList = gradeSelectList;

            ViewBag.Classes = await InstitutionManager.GetAllClassesAsync();

            if (ModelState.IsValid)
            {
                var grade = await InstitutionManager.FindGradeByIdAsync(model.GradeId);

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

                if (major == null)
                {
                    ModelState.AddModelError("", "找不到major");
                    return(View(model));
                }
                var class1 = new Class
                {
                    ClassDefination = model.Defination,
                    Grade           = grade,
                    Major           = major
                };
                if (await InstitutionManager.IsClassExistAsync(c =>
                                                               !c.IsDeleted && c.Grade.Id == class1.Grade.Id && c.Major.Id == class1.Major.Id &&
                                                               c.ClassDefination == class1.ClassDefination))
                {
                    ModelState.AddModelError("", "已存在Class");
                    return(View(model));
                }
                await InstitutionManager.AddClassAsync(class1);

                return(RedirectToAction("AllClass"));
            }
            return(View(model));
        }
        public async Task <ActionResult> DeleteGrade(int?id)
        {
            if (ModelState.IsValid)
            {
                var model = await InstitutionManager.FindGradeByIdAsync(id ?? 0);

                if (model != null)
                {
                    await InstitutionManager.DeleteGradeAsync(model);
                }
            }
            return(RedirectToAction("AllGrade"));
        }
        public async Task <ActionResult> EditGrade(int?id)
        {
            var grade = await InstitutionManager.FindGradeByIdAsync(id ?? 0);

            if (grade == null)
            {
                return(HttpNotFound());
            }
            var grades = await InstitutionManager.GetAllGradesAsync();

            ViewBag.Grades = grades;
            return(View(new GradeEditViewModel {
                GradeNumber = grade.GradeNum, Id = grade.Id
            }));
        }
        public async Task <ActionResult> EditGrade(GradeEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var grade = await InstitutionManager.FindGradeByIdAsync(model.Id);

                if (grade == null)
                {
                    ModelState.AddModelError("", "未找到此项grade");
                    return(View(model));
                }
                grade.GradeNum = model.GradeNumber;
                await InstitutionManager.UpdateGradeAsync(grade);

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