public async Task <ActionResult> Create() { var departments = await DepartmentManager.GetAllEntities().ToListAsync(); SelectList departSelectList = new SelectList(departments, "Id", "Name"); ViewBag.departSelectList = departSelectList; var majors = await MajorManager.GetMajorsForDepartment(departments.ElementAtOrDefault(0)).ToListAsync(); SelectList majorSelectList = new SelectList(majors, "Id", "Name"); ViewBag.majorSelectList = majorSelectList; var grades = await GradeManager.GetAllEntities().ToListAsync(); SelectList gradeSelectList = new SelectList(grades, "Id", "GradeNum"); ViewBag.gradeSelectList = gradeSelectList; var teachers = (await UserManager.GetNoClassTeachersAsync()).ToList(); ViewBag.teacherSelectList = new SelectList(teachers, "Id", "Name"); ViewBag.Classes = await ClassManager.GetAllEntities().ToListAsync(); return(View()); }
public async Task <ActionResult> Edit(int?id) { var class1 = await ClassManager.FindEntityByIdAsync(id ?? 0); if (class1 == null) { return(HttpNotFound("class不存在")); } var departments = await DepartmentManager.GetAllEntities().ToListAsync(); SelectList departSelectList = new SelectList(departments.ToList(), "Id", "Name"); ViewBag.departSelectList = departSelectList; var majors = await MajorManager.GetMajorsForDepartment(departments.ElementAtOrDefault(0)).ToListAsync(); SelectList majorSelectList = new SelectList(majors.ToList(), "Id", "Name"); ViewBag.majorSelectList = majorSelectList; var grades = await GradeManager.GetAllEntities().ToListAsync(); SelectList gradeSelectList = new SelectList(grades.ToList(), "Id", "GradeNum"); ViewBag.gradeSelectList = gradeSelectList; var teachers = (await UserManager.GetNoClassTeachersAsync()).ToList(); if (class1.ClassTeacher != null) { teachers.Add(class1.ClassTeacher.Teacher); } ViewBag.teacherSelectList = new SelectList(teachers, "Id", "Name"); ViewBag.Classes = await ClassManager.GetAllEntities().ToListAsync(); var model = new ClassEditViewModel { Defination = class1.ClassDefination, DepartmentId = class1.Major.Department.Id, GradeId = class1.Grade.Id, MajorId = class1.Major.Id, Students = await UserManager.GetStudentsOfClassAsync(class1).ToListAsync(), ClassTeacherId = class1.ClassTeacher == null ? null : class1.ClassTeacher.Teacher.Id }; return(View(model)); }
public async Task <ActionResult> Edit(ClassEditViewModel model) { var departments = await DepartmentManager.GetAllEntities().ToListAsync(); SelectList departSelectList = new SelectList(departments.ToList(), "Id", "Name"); ViewBag.departSelectList = departSelectList; var majors = await MajorManager.GetMajorsForDepartment(departments.ElementAtOrDefault(0)).ToListAsync(); SelectList majorSelectList = new SelectList(majors.ToList(), "Id", "Name"); ViewBag.majorSelectList = majorSelectList; var grades = await GradeManager.GetAllEntities().ToListAsync(); SelectList gradeSelectList = new SelectList(grades.ToList(), "Id", "GradeNum"); ViewBag.gradeSelectList = gradeSelectList; var teachers = (await UserManager.GetNoClassTeachersAsync()).ToList(); ViewBag.teacherSelectList = new SelectList(teachers, "Id", "Name"); ViewBag.Classes = await ClassManager.GetAllEntities().ToListAsync(); if (ModelState.IsValid) { var class1 = await ClassManager.FindEntityByIdAsync(model.Id); if (class1 == null) { ModelState.AddModelError("", "找不到class1"); return(View(model)); } var grade = await GradeManager.FindEntityByIdAsync(model.GradeId); if (grade == null) { ModelState.AddModelError("", "找不到grade"); return(View(model)); } var major = await MajorManager.FindEntityByIdAsync(model.MajorId); if (major == null) { ModelState.AddModelError("", "找不到major"); return(View(model)); } var teacher = await UserManager.FindByIdAsync(model.ClassTeacherId); if (teacher == null) { ModelState.AddModelError("", "找不到teacher"); return(View(model)); } class1.Major = major; class1.Grade = grade; class1.ClassDefination = model.Defination; class1.ClassTeacher = teacher.TeacherInfo; await ClassManager.UpdateEntityAsync(class1); teacher.TeacherInfo.ManageClass = class1; await UserManager.UpdateAsync(teacher); await UserManager.RemoveStudentsFromClassAsync(model.IdsToRemove ?? new string[] { }); return(RedirectToAction("Index")); } return(View(model)); }
public async Task <ActionResult> Create(ClassCreateViewModel model) { if (ModelState.IsValid) { var departments = await DepartmentManager.GetAllEntities().ToListAsync(); SelectList departSelectList = new SelectList(departments, "Id", "Name"); ViewBag.departSelectList = departSelectList; var majors = await MajorManager.GetMajorsForDepartment(departments.ElementAtOrDefault(0)).ToListAsync(); SelectList majorSelectList = new SelectList(majors, "Id", "Name"); ViewBag.majorSelectList = majorSelectList; var grades = await GradeManager.GetAllEntities().ToListAsync(); SelectList gradeSelectList = new SelectList(grades, "Id", "GradeNum"); ViewBag.gradeSelectList = gradeSelectList; var teachers = (await UserManager.GetNoClassTeachersAsync()).ToList(); ViewBag.teacherSelectList = new SelectList(teachers, "Id", "Name"); ViewBag.Classes = await ClassManager.GetAllEntities().ToListAsync(); var grade = await GradeManager.FindEntityByIdAsync(model.GradeId); if (grade == null) { ModelState.AddModelError("", "找不到grade"); return(View(model)); } var major = await MajorManager.FindEntityByIdAsync(model.MajorId); if (major == null) { ModelState.AddModelError("", "找不到major"); return(View(model)); } var teacher = await UserManager.FindByIdAsync(model.ClassTeacherId); if (teacher == null) { ModelState.AddModelError("", "找不到teacher"); return(View(model)); } var class1 = new Class { ClassDefination = model.Defination, Grade = grade, Major = major, ClassTeacher = teacher.TeacherInfo }; if (await ClassManager.IsEntityExistByLamdaAsync(c => c.Grade.Id == class1.Grade.Id && c.Major.Id == class1.Major.Id && c.ClassDefination == class1.ClassDefination)) { ModelState.AddModelError("", "已存在Class"); return(View(model)); } var class2 = await ClassManager.AddEntityAsync(class1); teacher.TeacherInfo.ManageClass = class2; await UserManager.UpdateAsync(teacher); return(RedirectToAction("Index")); } return(View(model)); }