public async Task <IActionResult> Create(CreateStaffViewModel model) { var results = await _userManager.FindByEmailAsync(model.Email); if (ModelState.IsValid && results == null) { var staff = new Staff() { Forename = model.Forename, MiddleNames = model.MiddleNames, Surname = model.Surname, Email = model.Email, UserName = model.Email, DateOfBirth = model.DateOfBirth, TimeOfRegistration = DateTime.Now, EmailConfirmed = true }; await _userManager.CreateAsync(staff, model.Password); await _userManager.AddToRoleAsync(staff, model.Role); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["Roles"] = new SelectList(_context.Roles.Where(r => !r.Name.Equals("Customer")), "Name", "Name"); return(View(model)); }
public async Task <IActionResult> CheckNo([Bind(include: "No")] CreateStaffViewModel model) { string message = null; var stf = await _mainRepository.QueryByNoAsync(model.No); if (stf.Count > 0) { message = "工号" + model.No + "已被注册"; return(Json(message)); } else { return(Json(true)); } }
public ActionResult Detail(long?id) { var staff = _db.Staff.FirstOrDefault(p => p.Id == id); if (staff == null) { return(Redirect("Index")); } CreateStaffViewModel model = new CreateStaffViewModel() { StaffName = staff.StaffName }; return(View(model)); }
public ActionResult Create(CreateStaffViewModel model) { if (!ModelState.IsValid) { ModelState.AddModelError("Error", "Error! Please try again!"); return(View(model)); } Staff staff = new Staff() { StaffName = model.StaffName }; _db.Staff.Add(staff); _db.SaveChanges(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Create(CreateStaffViewModel model) { var stf = await _mainRepository.QueryByNoAsync(model.No); if (stf.Count > 0) { ModelState.AddModelError("No", "服务端验证:该工号" + model.No + "已被注册"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { await _mainRepository.CreateAsync(new Staff { Id = Guid.NewGuid(), No = model.No, Name = model.Name, Gender = (int)model.Gender, OfficePhone = model.OfficePhone, MobilePhone = model.MobilePhone, Position = (int)(model.Position), JobTitle = (int)(model.JobTitle), Education = (int)(model.Education), HiredDate = model.HiredDate }); TempData["globalMessage"] = "成功创建工号" + model.No + "职工"; } catch (DbUpdateException /* ex */) { // Log the error(uncomment ex variable name and write a log. ModelState.AddModelError("", "无法新建。 " + "请重试, 如果该问题仍然存在 " + "请联系系统管理员。"); } return(View()); }
public async System.Threading.Tasks.Task <IHttpActionResult> Post([FromBody] CreateStaffViewModel vm) { if (vm != null) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } SetPrincipal(); var result = await _manager.CreateUser(vm); if (result.Result == true) { return(Ok(result.SingleResult)); } } return(BadRequest()); }
public ActionResult Detail(long?id, CreateStaffViewModel model) { if (!ModelState.IsValid) { ModelState.AddModelError("Error", "Error! Please try again!"); return(View(model)); } var staff = _db.Staff.FirstOrDefault(p => p.Id == id); if (staff == null) { return(Redirect("Index")); } staff.StaffName = model.StaffName; _db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create(CreateStaffViewModel staff, HttpPostedFileBase file) { try { if (ModelState.IsValid) { var memberToAdd = new Staff() { Title = staff.Title, FirstName = staff.FirstName, LastName = staff.LastName, Email = staff.Email, Description = staff.Description }; if (file != null) { var imageBeforeResize = Image.FromStream(file.InputStream); var imageAfterResize = ResizeImage(imageBeforeResize, 400, 500); var resizedByteArray = ImageToByte(imageAfterResize); memberToAdd.PhotoContent = resizedByteArray; memberToAdd.MimeType = file.ContentType; } _db.Staff.Add(memberToAdd); _db.SaveChanges(); } return(RedirectToAction("Index")); } catch { return(View()); } }