public ActionResult Create(Staff staff) { if (ModelState.IsValid) { /*Step1:先保存员工固定字段*/ db.Staffs.Add(staff); db.SaveChanges(); /*查找员工基本信息表预留字段(name)*/ var fieldList1 = (from p in db.ReserveFields where p.TableName == "Staffs" select p).ToList(); ViewBag.fieldList = fieldList1; /*遍历,保存员工基本信息预留字段*/ foreach (var temp in fieldList1) { StaffReserve sr = new StaffReserve(); sr.Number = staff.Number; sr.FieldId = temp.Id; sr.Value = Request[temp.FieldName]; /*占位,为了在Index中显示整齐的格式*/ if (sr.Value == null) sr.Value = " "; db.StaffReserves.Add(sr); db.SaveChanges(); } return RedirectToAction("Index"); } return View(staff); }
public ActionResult Create(Staff staff) { if (ModelState.IsValid) { var tmp = db.Staffs.Where(p => p.StaffNumber.Equals(staff.StaffNumber)).ToList(); if (tmp.Count != 0) { ModelState.AddModelError("", "抱歉,该工号已经被注册!"); return View(staff); } else { /*Step1:先保存员工固定字段*/ db.Staffs.Add(staff); db.SaveChanges(); /*查找员工基本信息表预留字段(name)*/ var fieldList = (from p in db.ReserveFields where p.TableName == "Staffs" select p).ToList(); ViewBag.fieldList = fieldList; /*遍历,保存员工基本信息预留字段*/ foreach (var temp in fieldList) { StaffReserve sr = new StaffReserve(); sr.Number = staff.Number; sr.FieldId = temp.Id; sr.Value = Request[temp.FieldName]; /*占位,为了在Index中显示整齐的格式*/ if (sr.Value == null) sr.Value = " "; db.StaffReserves.Add(sr); db.SaveChanges(); } return RedirectToAction("Index"); } } return View(staff); }