public ActionResult Create(Department department) { //模型状态错误(为空) if (ModelState.IsValid) { /*选出ReserveFields中部门相关的记录*/ var recordList = (from p in db.ReserveFields where p.TableName == "Departments" select p).ToList(); ViewBag.recordList = recordList; /*生成部门编号*/ department.Number = (new Random().Next(1111, 9999)).ToString(); /*遍历*/ foreach (var temp in recordList) { DepartmentReserve dr = new DepartmentReserve(); dr.Number = department.Number; dr.FieldId = temp.Id; dr.Value = Request[temp.FieldName]; db.DepartmentReserves.Add(dr); /*把这行去掉之后即可运行*/ //db.SaveChanges(); } db.Departments.Add(department); db.SaveChanges(); return RedirectToAction("Index"); } return View(department); }
public ActionResult Create(Department department) { if (ModelState.IsValid) { /*Step1:如果上级部门为空则上级部门编号为公司Id*/ if (department.ParentDepartmentId == null) { department.ParentDepartmentId = this.CompanyId; } /*Step2:部门编号唯一,应该用Ajax??*/ //var find = (from p in db.Departments where p.DepartmentId == department.DepartmentId select p).FirstOrDefault(); //if (find != null) { ModelState.AddModelError("","部门编号已存在!" );} /*Step3:保存固定字段(为了生成主键Id)*/ db.Departments.Add(department); db.SaveChanges(); /*Step4:显示预留字段名称*/ var recordList = (from p in db.ReserveFields where p.TableName == "Departments" select p).ToList(); ViewBag.recordList = recordList; /*Step5:保存预留字段的值*/ foreach (var temp in recordList) { DepartmentReserve dr = new DepartmentReserve(); dr.Number = department.Id; dr.FieldId = temp.Id; dr.Value = Request[temp.FieldName]; /*占位,为了在Index中显示整齐的格式*/ if (dr.Value == null) dr.Value = " "; db.DepartmentReserves.Add(dr); db.SaveChanges(); } return RedirectToAction("Index"); } return View(department); }
public ActionResult Create([Bind(Include = "Id,TableName,FieldName,Description,Status")] ReserveField reserveField) { List<SelectListItem> items = new List<SelectListItem>(); items.Add(new SelectListItem { Text = "部门信息表", Value = "Departments" }); items.Add(new SelectListItem { Text = "员工档案表", Value = "Staffs" }); items.Add(new SelectListItem { Text = "员工技能表", Value = "StaffSkills" }); //items.Add(new SelectListItem { Text = "员工技能表", Value = "StaffSkills" }); items.Add(new SelectListItem { Text = "人事变更申请表", Value = "StaffChanges" }); items.Add(new SelectListItem { Text = "离职申请表", Value = "StaffApplications" }); items.Add(new SelectListItem { Text = "离职档案表", Value = "StaffArchives" }); items.Add(new SelectListItem { Text = "合同管理表", Value = "Contracts" }); ViewBag.List = items; if (ModelState.IsValid) { var number = db.ReserveFields.Select(rf=>rf.Id).Max(); reserveField.Id = number+1; db.ReserveFields.Add(reserveField); /*状态设置为有效*/ reserveField.Status = "true"; db.SaveChanges(); //if (reserveField.TableName == "Contracts") //{ // StaffApplicationReserve sar = new ContractReserve(); // var staffApplication = db.StaffApplications.ToList(); // foreach (var temp in staffApplication) // { // sar.FieldId = reserveField.Id; // sar.Number = temp.Id; // sar.Value = " "; // db.StaffApplicationReserves.Add(sar); // db.SaveChanges(); // } //} //确保固定字段表、变化字段表和变化字段存储表的数据一致性。 if (reserveField.TableName == "StaffApplications") { StaffApplicationReserve sar = new StaffApplicationReserve(); var staffApplication = db.StaffApplications.ToList(); foreach (var temp in staffApplication) { sar.FieldId = reserveField.Id; sar.Number = temp.Id; sar.Value = " "; db.StaffApplicationReserves.Add(sar); db.SaveChanges(); } } if (reserveField.TableName == "StaffChanges") { StaffChangeReserve scr = new StaffChangeReserve(); var staffchange = db.StaffChanges.ToList(); foreach (var temp in staffchange) { scr.FieldId = reserveField.Id; scr.Number = temp.Id; scr.Value = " "; db.StaffChangeReserves.Add(scr); db.SaveChanges(); } } if (reserveField.TableName == "StaffSkills") { StaffSkillReserve ssr = new StaffSkillReserve(); var staffskill = db.StaffSkills.ToList(); foreach (var temp in staffskill) { ssr.FieldId = reserveField.Id; ssr.Number = temp.Id; ssr.Value = " "; db.StaffSkillReserves.Add(ssr); db.SaveChanges(); } } if (reserveField.TableName == "Staffs") { StaffReserve sr = new StaffReserve(); var staff = db.Staffs.ToList(); foreach (var temp in staff) { sr.FieldId = reserveField.Id; sr.Number = temp.Number; sr.Value = " "; db.StaffReserves.Add(sr); db.SaveChanges(); } } if (reserveField.TableName == "Departments") { DepartmentReserve dr = new DepartmentReserve(); var department = db.Departments.ToList(); foreach(var temp in department) { dr.FieldId=reserveField.Id; dr.Number = temp.Id; dr.Value = " "; db.DepartmentReserves.Add(dr); db.SaveChanges(); } } if (reserveField.TableName == "StaffArchives") { StaffArchiveReserve sar = new StaffArchiveReserve(); var staffarchive = db.StaffArchives.ToList(); foreach (var temp in staffarchive) { sar.FieldId = reserveField.Id; sar.Number = temp.Id; sar.Value = " "; db.StaffArchiveReserves.Add(sar); db.SaveChanges(); } } return RedirectToAction("IndexInfo", new { table = reserveField.TableName }); } return View(reserveField); }