public ActionResult SetDetail() { int id = Request.Form["PositionId"].ParseInt(); TblPosition ob = uow.Modules.Position.Get(id); if (ob.PositionId <= 0) { ob.CreatedBy = CurrentUID; ob.CreatedDate = CurrentDate; } ob.PositionName = Request.Form["PositionName"]; ob.UpdatedBy = CurrentUID; ob.UpdatedDate = CurrentDate; try { //Validate model b4 save uow.Modules.Position.Set(ob); uow.SaveChanges(); return(RedirectToAction("Detail", new { area = "", controller = "Position", msg = "บันทึกข้อมูลเรียบร้อยแล้ว", msgType = AlertMsgType.Success })); } catch (Exception ex) { string msg = ex.GetMessage(true); return(ViewDetail(ob, msg, AlertMsgType.Danger)); } }
private ActionResult ViewDetail(TblPosition ob, string msg, AlertMsgType?msgType) { try { if (ob == null) { throw new Exception("ไม่พบข้อมูลที่ต้องการ, กรุณาลองใหม่อีกครั้ง"); } if (!string.IsNullOrWhiteSpace(msg)) { WidgetAlertModel alert = new WidgetAlertModel() { Message = msg }; if (msgType.HasValue) { alert.Type = msgType.Value; } ViewBag.Alert = alert; } return(View(ob)); } catch (Exception ex) { return(RedirectToAction("Detail", MVCController, new { msg = ex.GetMessage(), msgType = AlertMsgType.Danger })); } }
public ActionResult Delete() { try { int id = Request.Form["PositionId"].ParseInt(); TblPosition ob = uow.Modules.Position.Get(id); if (ob == null) { return(RedirectToAction("Index", MVCController, new { msg = "ไม่พบข้อมูลที่ต้องการ", msgType = AlertMsgType.Warning })); } uow.Modules.Position.Delete(ob); uow.SaveChanges(); return(RedirectToAction("Index", MVCController, new { msg = "ลบข้อมูลเรียบร้อยแล้ว", msgType = AlertMsgType.Success })); } catch (Exception ex) { return(RedirectToAction("Index", MVCController, new { msg = ex.GetMessage(), msgType = AlertMsgType.Danger })); } }
public IActionResult Put(string id, [FromBody] TblPosition uPosition) { return(new JsonResult(this.repository.UpdateAsync(uPosition, id).Result, this.DefaultJsonSettings)); }
public IActionResult Post([FromBody] TblPosition nPosition) { return(new JsonResult(this.repository.AddAsync(nPosition).Result, this.DefaultJsonSettings)); }
public ActionResult Detail(int?id, string msg, AlertMsgType?msgType) { TblPosition ob = uow.Modules.Position.Get(id ?? 0); return(ViewDetail(ob, msg, msgType)); }
public async Task <IActionResult> NewEmployee(EmployeeModel employeeModel) { var person = _context.TblPerson.Where(x => x.EmailAddress == employeeModel.EmailAddress).ToList(); ViewData["Error"] = ""; foreach (var x in person) { if (x.EmailAddress == employeeModel.EmailAddress) { ViewData["Position"] = new SelectList(_context.LkpPositionTypes.Where(s => s.IsActive == true), "PositionId", "Description"); ViewData["Error"] = "The email address for username is already taken."; return(View()); } } if (ModelState.IsValid) { TblPerson tblPerson = new TblPerson { FirstName = employeeModel.FirstName, LastName = employeeModel.LastName, EmailAddress = employeeModel.EmailAddress, PhoneNumber = employeeModel.PhoneNumber, StreetNumber = employeeModel.StreetNumber, StreetName = employeeModel.StreetName, City = employeeModel.City, PostalCode = employeeModel.PostalCode, Country = employeeModel.Country, }; await _context.TblPerson.AddAsync(tblPerson); TblEmployee tblEmployee = new TblEmployee { EmployeeId = tblPerson.PersonId, EmergencyContactName = employeeModel.EmergencyContactName, EmergencyContactNumber = employeeModel.EmergencyContactNumber, }; await _context.TblEmployee.AddAsync(tblEmployee); TblPosition tblPosition = new TblPosition { EmployeeId = tblEmployee.EmployeeId, PositionId = employeeModel.Position, StartDate = DateTime.Now, }; await _context.TblPosition.AddAsync(tblPosition); var user = new ApplicationUser { UserName = employeeModel.EmailAddress, Email = employeeModel.EmailAddress }; var result = await _userManager.CreateAsync(user, "Password@123"); if (result.Succeeded) { _userManager.AddToRoleAsync(user, _context.LkpPositionTypes.Find(employeeModel.Position).Description.ToString()).Wait(); } await _context.SaveChangesAsync(); return(Redirect("/Admin/")); } ViewData["Position"] = new SelectList(_context.LkpPositionTypes.Where(s => s.IsActive == true), "PositionId", "Description"); return(View()); }
public async Task <IActionResult> UpdateEmployee(int?id, EmployeeModel employeeModel) { if (id != employeeModel.PersonID) { return(NotFound()); } if (ModelState.IsValid) { TblPerson tblPerson = new TblPerson { PersonId = employeeModel.PersonID, FirstName = employeeModel.FirstName, LastName = employeeModel.LastName, EmailAddress = employeeModel.EmailAddress, PhoneNumber = employeeModel.PhoneNumber, StreetNumber = employeeModel.StreetNumber, StreetName = employeeModel.StreetName, City = employeeModel.City, PostalCode = employeeModel.PostalCode, Country = employeeModel.Country, }; TblEmployee tblEmployee = new TblEmployee { EmployeeId = employeeModel.PersonID, EmergencyContactName = employeeModel.EmergencyContactName, EmergencyContactNumber = employeeModel.EmergencyContactNumber, }; _context.Update(tblPerson); _context.Update(tblEmployee); await _context.SaveChangesAsync(); var position = _context.TblPosition.Where(x => x.EmployeeId == employeeModel.PersonID && x.EndDate == null).ToList(); var positionID = 0; var previousRecordID = 0; foreach (var x in position) { previousRecordID = x.Id; positionID = x.PositionId; } if (positionID != employeeModel.Position) { var previousRecord = _context.TblPosition.Find(previousRecordID); previousRecord.EndDate = DateTime.Now; _context.Update(previousRecord); TblPosition tblPosition = new TblPosition { EmployeeId = employeeModel.PersonID, PositionId = employeeModel.Position, StartDate = DateTime.Now, }; await _context.TblPosition.AddAsync(tblPosition); var user = _userManager.Users.Where(x => x.UserName == employeeModel.EmailAddress).ToList(); foreach (var x in user) { var previousUser = _dbContext.Users.Find(x.Id); var removeUser = await _userManager.RemoveFromRoleAsync(previousUser, _context.LkpPositionTypes.Find(positionID).Description.ToString()); if (removeUser.Succeeded) { await _userManager.AddToRoleAsync(previousUser, _context.LkpPositionTypes.Find(employeeModel.Position).Description.ToString()); } } await _context.SaveChangesAsync(); } return(Redirect("/Admin/Employee")); } ViewData["Position"] = new SelectList(_context.LkpPositionTypes.Where(s => s.IsActive == true), "PositionId", "Description"); return(View(employeeModel)); }