public ActionResult CreateTeacher(CourseTeacher teacher) { if (ModelState.IsValid) { teacher.savingPhoto(); db.CourseTeacher.Add(teacher); db.SaveChanges(); var result = new { success = true }; return(Json(result)); } else { var result = new { success = false, errorMessage = "資料有誤,請檢查並更正資料", ModelStateErrors = ModelState.Where(x => x.Value.Errors.Count > 0) .ToDictionary(k => k.Key, k => k.Value.Errors.Select(e => e.ErrorMessage).ToArray()) }; return(Json(result)); } }
public ActionResult EditTeacher(CourseTeacher teacher) { if (ModelState.IsValid) { teacher.courses = db.CourseTeacher.Where(t => t.sqno == teacher.sqno).SelectMany(t => t.courses).ToList(); teacher.savingPhoto(); db.Entry(teacher).State = EntityState.Modified; db.SaveChanges(); var result = new { success = true }; return(Json(result)); } else { var result = new { success = false, errorMessage = "資料有誤,請檢查並更正資料", ModelStateErrors = ModelState.Where(x => x.Value.Errors.Count > 0) .ToDictionary(k => k.Key, k => k.Value.Errors.Select(e => e.ErrorMessage).ToArray()) }; return(Json(result)); } }