public async Task <ActionResult> EditMajor(MajorEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var major = await InstitutionManager.FindMajorByIdAsync(model.Id);

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

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

                return(RedirectToAction("AllMajor"));
            }
            return(View(model));
        }
        public async Task <ActionResult> EditDepartment(DepartmentEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var de = await InstitutionManager.FindDepartmentByIdAsync(model.Id);

                if (de != null)
                {
                    if (!string.IsNullOrEmpty(model.Name))
                    {
                        de.Name = model.Name;
                    }
                    await InstitutionManager.UpdateDepartmentAsync(de);

                    await InstitutionManager.AddMajorsToDepartmentAsync(de, model.IdsToAdd ?? new int[] { });

                    await InstitutionManager.RemoveMajorsFromDepartmentAsync(de, model.IdsToRemove ?? new int[] { });

                    return(RedirectToAction("AllDepartment"));
                }
                else
                {
                    ModelState.AddModelError("", "学院不存在");
                }
            }
            return(View(model));
        }
        public async Task <JsonResult> GetMajorOfDepartment(int departId)
        {
            var depart = await InstitutionManager.FindDepartmentByIdAsync(departId);

            if (depart == null)
            {
                return(Json(new { error = 1, message = "找不到该Department" }));
            }
            var s = Json(new { error = 0, message = "获取成功", data = depart.Majors.Select(m => new { m.Id, m.Name }) });

            return(s);
        }
        public async Task <ActionResult> DeleteDepartment(int id)
        {
            if (ModelState.IsValid)
            {
                var major = await InstitutionManager.FindDepartmentByIdAsync(id);

                if (major != null)
                {
                    await InstitutionManager.DeleteDepartmentAsync(major);
                }
            }
            return(RedirectToAction("AllDepartment"));
        }
        public async Task <ActionResult> EditDepartment(int id)
        {
            var de = await InstitutionManager.FindDepartmentByIdAsync(id);

            if (de == null)
            {
                return(HttpNotFound());
            }
            ViewBag.InMajors = await InstitutionManager.GetMajorsForDepartmentAsync(de);

            ViewBag.OutMajors = await InstitutionManager.GeNoDepartmentMajorsAsync(de);

            var model = new DepartmentEditViewModel {
                Id = de.Id, Name = de.Name
            };

            return(View(model));
        }
        public async Task <ActionResult> CreateMajor(MajorCreateViewModel major)
        {
            if (ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(major.MajorName))
                {
                    ModelState.AddModelError("", "MajorName不能为空");
                }
                else if (major.DepartmentId == 0)
                {
                    ModelState.AddModelError("", "DepartmentId不能为空");
                }
                else
                {
                    var model = await InstitutionManager.FindMajorByNameAsync(major.MajorName);

                    if (model == null)
                    {
                        await InstitutionManager.AddMajorAsync(major.MajorName);

                        model = await InstitutionManager.FindMajorByNameAsync(major.MajorName);

                        var depart = await InstitutionManager.FindDepartmentByIdAsync(major.DepartmentId);

                        if (depart == null)
                        {
                            ModelState.AddModelError("", "找不到对应学院");
                        }
                        await InstitutionManager.AddMajorsToDepartmentAsync(depart, new[] { model.Id });

                        return(RedirectToAction("AllMajor"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "已有相同名称");
                    }
                }
            }
            return(View(major));
        }