public ActionResult EditEmployeeFamily() { ViewBag.Path1 = "家庭信息>"; var employee = db.Employee.Where(a => a.UserId == WebSecurity.CurrentUserId).SingleOrDefault(); if (employee == null) { return(HttpNotFound()); } if (employee.EmployeeStatus != EmployeeStatus.新增已通知) { return(RedirectToAction("FrontDetail")); } var editEmployeeFamily = new EditEmployeeFamily { EmployeeId = employee.Id }; Mapper.CreateMap <Family, EditFamily>().ForMember(x => x.FamilyId, o => o.MapFrom(s => s.Id)); var list = Mapper.Map <ICollection <Family>, ICollection <EditFamily> >(employee.GetFamilies()); editEmployeeFamily.EditFamilies = list; return(View(editEmployeeFamily)); }
public ActionResult EditEmployeeFamily(EditEmployeeFamily editEmployeeFamily) { ViewBag.Path1 = "家庭信息>"; var employee = db.Employee.Include(a => a.Families).Where(a => a.UserId == WebSecurity.CurrentUserId && a.Id == editEmployeeFamily.EmployeeId).SingleOrDefault(); if (employee == null) { return(HttpNotFound()); } if (employee.EmployeeStatus != EmployeeStatus.新增已通知) { return(RedirectToAction("FrontDetail")); } if (ModelState.IsValid) { var old = new HashSet <int>(employee.GetFamilies().Select(a => a.Id)); var cur = new HashSet <int>(editEmployeeFamily.EditFamilies.Where(a => a.Delete == false).Select(a => a.FamilyId)); // 取得不在最新列表中的记录删除 var del = (from a in old where !(cur.Contains(a)) select a).ToList(); foreach (var i in del) { var e = db.Family.Find(i); db.Family.Remove(e); } // end // 取得在最新列表中的记录更新 var upd = (from a in old where cur.Contains(a) select a).ToList(); foreach (var i in upd) { var e1 = db.Family.Find(i); var e2 = editEmployeeFamily.EditFamilies.Where(a => a.FamilyId == i).Single(); e1.Name = e2.Name; e1.Relation = e2.Relation; e1.Sex = e2.Sex.Value; e1.Company = e2.Company; e1.Position = e2.Position; e1.Phone = e2.Phone; } // end // 取得在最新列表中的记录添加 var add = editEmployeeFamily.EditFamilies.Where(a => a.Delete == false && a.FamilyId == 0); foreach (var i in add) { var e = new Family { Name = i.Name, Relation = i.Relation, Sex = i.Sex.Value, Company = i.Company, Position = i.Position, Phone = i.Phone }; employee.Families.Add(e); } // end db.PPSave(); return(RedirectToAction("EditEmployeeDoc")); } return(View(editEmployeeFamily)); }