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