コード例 #1
0
ファイル: FamilyController.cs プロジェクト: pennsong/OB
        public ActionResult Create(Family family)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    db.Family.Add(family);
                    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", family.EmployeeId);
            return View(family);
        }
コード例 #2
0
ファイル: EmployeeController.cs プロジェクト: pennsong/OB
        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);
        }