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> DeleteClass(int?id)
        {
            if (ModelState.IsValid)
            {
                var model = await InstitutionManager.FindClassByIdAsync(id ?? 0);

                if (model != null)
                {
                    await InstitutionManager.DeleteClassAsync(model);
                }
            }
            return(RedirectToAction("AllClass"));
        }
        public async Task <ActionResult> AddStudent(AddStudentsViewModel model)
        {
            if (ModelState.IsValid)
            {
                var class1 = await InstitutionManager.FindClassByIdAsync(model.ClassId);

                if (class1 == null)
                {
                    return(View("Error", new[] { "班级找不到" }));
                }
                await UserManager.AddStudentsToClassAsync(class1, model.IdsToAdd ?? new string[] { });

                model.IdsToAdd = new string[] { };
                return(RedirectToAction("AddStudent"));
            }
            return(View(model));
        }
        public async Task <ActionResult> AddStudent(int?id, int?p)
        {
            var class1 = await InstitutionManager.FindClassByIdAsync(id ?? 0);

            if (class1 == null)
            {
                ModelState.AddModelError("id", "The Class you are looking for is not Exist");
                return(View("Error", new[] { "The Class you are looking for is not Exist" }));
                //return HttpNotFound("The Class you are looking for is not Exist");
            }
            var students = UserManager.GetNoClassStudents().ToList().ToPagedList(p ?? 1, 10);

            ViewBag.OutStudents = students;
            ViewBag.Class       = class1;

            return(View());
        }
        public async Task <ActionResult> EditClass(int?id)
        {
            var class1 = await InstitutionManager.FindClassByIdAsync(id ?? 0);

            if (class1 == null)
            {
                return(HttpNotFound("class不存在"));
            }

            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();

            var model = new ClassEditViewModel
            {
                Defination   = class1.ClassDefination,
                DepartmentId = class1.Major.Department.Id,
                GradeId      = class1.Grade.Id,
                MajorId      = class1.Major.Id,
                Students     = class1.Students
            };

            return(View(model));
        }