public ActionResult Create([Bind(Include = "ID,Name,Sex,Department,PhoneNumber,EnterDate")] Staff staff) { if (ModelState.IsValid) { if (staff.Sex != "男" && staff.Sex != "女") { staff.SexJudge = "性别填写存在错误,只能是男女"; } //电话号码判断 Regex RegMobilePhone = new Regex("^1[3|5|8][0-9]{9}$"); if (staff.PhoneNumber.Length != 11 || !RegMobilePhone.IsMatch(staff.PhoneNumber)) { staff.PhoneNumberJudge = "电话号码填写存在错误"; } //页面返回 if (staff.SexJudge != null || staff.PhoneNumberJudge != null) { return(View(staff)); } else { db.Staffs.Add(staff); db.SaveChanges(); return(RedirectToAction("Index")); } } return(View(staff)); }
public ActionResult Create([Bind(Include = "StaffId,StaffUsername,StaffPassword")] Staff staff) { if (ModelState.IsValid) { db.Staffs.Add(staff); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(staff)); }
public ActionResult EditStaff(Staff staff) { using (var context = new StaffDbContext()) { if (staff.Id > 0) { // Edit var val = context.Staffs.Where(data => data.Id == staff.Id).FirstOrDefault(); if (val != null) { val.Name = staff.Name; val.Age = staff.Age; val.City = staff.City; } } else { //Save context.Staffs.Add(staff); } context.SaveChanges(); return(RedirectToAction("Index")); } }
public ActionResult CreateStaff(Staff new_staff) { using (var context = new StaffDbContext()) { context.Staffs.Add(new_staff); //saving changes context.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult ConfirmDeleteStaff(int?id) { if (id == null) { return(HttpNotFound()); } using (var context = new StaffDbContext()) { var value = context.Staffs.Where(data => data.Id == id).FirstOrDefault(); context.Staffs.Remove(value); context.SaveChanges(); return(RedirectToAction("Index")); } }
public int CreateStaff(Staff staff) { context.Staffs.Add(staff); return(context.SaveChanges()); }