public ActionResult Create(Education education) { if (ModelState.IsValid) { try { db.Education.Add(education); db.PPSave(); return RedirectToAction("Index"); } catch (DbUpdateException ex) { if (ex.InnerException.InnerException.Message.Contains("Cannot insert duplicate key row")) { ModelState.AddModelError(string.Empty, "相同名称的记录已存在,保存失败!"); } } } ViewBag.EmployeeId = new SelectList(db.Employee, "Id", "Name", education.EmployeeId); return View(education); }
public ActionResult EditEmployeeEducation(EditEmployeeEducation editEmployeeEducation) { ViewBag.Path1 = "教育信息>"; var employee = db.Employee.Include(a => a.Educations).Where(a => a.UserId == WebSecurity.CurrentUserId && a.Id == editEmployeeEducation.EmployeeId).SingleOrDefault(); if (employee == null) { return HttpNotFound(); } if (employee.EmployeeStatus != EmployeeStatus.新增已通知) { return RedirectToAction("FrontDetail"); } if (ModelState.IsValid) { var old = new HashSet<int>(employee.GetEducations().Select(a => a.Id)); var cur = new HashSet<int>(editEmployeeEducation.EditEducations.Where(a => a.Delete == false).Select(a => a.EducationId)); // 取得不在最新列表中的记录删除 var del = (from a in old where !(cur.Contains(a)) select a).ToList(); foreach (var i in del) { var e = db.Education.Find(i); db.Education.Remove(e); } // end // 取得在最新列表中的记录更新 var upd = (from a in old where cur.Contains(a) select a).ToList(); foreach (var i in upd) { var e1 = db.Education.Find(i); var e2 = editEmployeeEducation.EditEducations.Where(a => a.EducationId == i).Single(); e1.School = e2.School; e1.Major = e2.Major; e1.Degree = e2.Degree.Value; e1.Begin = e2.Begin.Value; e1.End = e2.End; } // end // 取得在最新列表中的记录添加 var add = editEmployeeEducation.EditEducations.Where(a => a.Delete == false && a.EducationId == 0); foreach (var i in add) { var e = new Education { EmployeeId = employee.Id, School = i.School, Major = i.Major, Degree = i.Degree.Value, Begin = i.Begin.Value, End = i.End }; employee.Educations.Add(e); } // end db.PPSave(); return RedirectToAction("EditEmployeeWork"); } return View(editEmployeeEducation); }