public ActionResult Create(Work work) { if (ModelState.IsValid) { try { db.Work.Add(work); 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", work.EmployeeId); return View(work); }
public ActionResult EditEmployeeWork(EditEmployeeWork editEmployeeWork) { ViewBag.Path1 = "工作经历>"; var employee = db.Employee.Include(a => a.Works).Where(a => a.UserId == WebSecurity.CurrentUserId && a.Id == editEmployeeWork.EmployeeId).SingleOrDefault(); if (employee == null) { return HttpNotFound(); } if (employee.EmployeeStatus != EmployeeStatus.新增已通知) { return RedirectToAction("FrontDetail"); } if (ModelState.IsValid) { var old = new HashSet<int>(employee.GetWorks().Select(a => a.Id)); var cur = new HashSet<int>(editEmployeeWork.EditWorks.Where(a => a.Delete == false).Select(a => a.WorkId)); // 取得不在最新列表中的记录删除 var del = (from a in old where !(cur.Contains(a)) select a).ToList(); foreach (var i in del) { var e = db.Work.Find(i); db.Work.Remove(e); } // end // 取得在最新列表中的记录更新 var upd = (from a in old where cur.Contains(a) select a).ToList(); foreach (var i in upd) { var e1 = db.Work.Find(i); var e2 = editEmployeeWork.EditWorks.Where(a => a.WorkId == i).Single(); e1.Company = e2.Company; e1.Position = e2.Position; e1.Begin = e2.Begin.Value; e1.End = e2.End; e1.Contact = e2.Contact; e1.Phone = e2.Phone; e1.Note = e2.Note; } // end // 取得在最新列表中的记录添加 var add = editEmployeeWork.EditWorks.Where(a => a.Delete == false && a.WorkId == 0); foreach (var i in add) { var e = new Work { Company = i.Company, Position = i.Position, Begin = i.Begin.Value, End = i.End, Contact = i.Contact, Phone = i.Phone, Note = i.Note }; employee.Works.Add(e); } // end db.PPSave(); return RedirectToAction("EditEmployeeFamily"); } return View(editEmployeeWork); }