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