public ActionResult ChangeInfo(Employee data, HttpPostedFileBase fileImage = null) { if (data != null) { Employee getEmployee = HumanResourceBLL.GetEmployee(data.EmployeeID); data.PhotoPath = getEmployee.PhotoPath; data.Password = getEmployee.Password; data.Notes = getEmployee.Notes; if (fileImage != null) { string get = DateTime.Now.ToString("ddMMyyyhhmmss"); string fileExtension = Path.GetExtension(fileImage.FileName); string fileName = get + fileExtension; string path = Path.Combine(Server.MapPath("~/Images"), fileName); data.PhotoPath = fileName; fileImage.SaveAs(path); } if (fileImage == null) { data.PhotoPath = getEmployee.PhotoPath; } bool IsEmailExist = AccountBLL.IsEmailExist(getEmployee.Email, getEmployee.EmployeeID); if (IsEmailExist) { ModelState.AddModelError("errorEmailDuplicate1", "Email đã tồn tại. Vui lòng nhập email khác!"); return(RedirectToAction("Index")); } else { bool editUser = HumanResourceBLL.Employee_Update(data); } } return(RedirectToAction("Index")); }
[HttpPost] //lkhi submit dữ liệu public ActionResult Input(Employee model, HttpPostedFileBase file, string[] role = null) { string groupName = ""; if (role != null) { foreach (var eachRole in role) { if (groupName == "") { groupName = groupName + eachRole; } else { groupName = groupName + "," + eachRole; } } } model.GroupName = groupName; if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "Last Name required"); } if (string.IsNullOrEmpty(model.FirstName)) { ModelState.AddModelError("FirstName", "First Name required"); } if (string.IsNullOrEmpty(model.City)) { model.City = ""; } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.Title)) { model.Title = ""; } if (string.IsNullOrEmpty(model.Country)) { model.Country = ""; } if (string.IsNullOrEmpty(model.Email)) { model.Email = ""; } if (string.IsNullOrEmpty(model.HomePhone)) { model.HomePhone = ""; } if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } var dateTime = new DateTime(1900, 01, 01); var compareDatetime = DateTime.Compare(model.BirthDate, dateTime); if (compareDatetime < 0) { ModelState.AddModelError("BirthDate", "BirthDate is not format"); return(View(model)); } compareDatetime = DateTime.Compare(model.HireDate, dateTime); if (compareDatetime < 0) { ModelState.AddModelError("HireDate", "HireDate is not format"); return(View(model)); } var type = "Add"; if (model.EmployeeID != 0) { type = "Edit"; } if (type == "Add") { if (!HumanResourceBLL.Employee_CheckMail(model.Email, type)) { ModelState.AddModelError("Email", "Email already exist"); return(View(model)); } } if (string.IsNullOrEmpty(model.Password)) { ModelState.AddModelError("Password", "Password required"); } var fileName = ""; var typeFile = ""; if (file != null) { //kiểm tra loại của file fileName = Path.GetFileName(file.FileName); typeFile = fileName.Substring(fileName.IndexOf('.')); if (typeFile != ".png" && typeFile != ".jpg" && typeFile != ".jpeg" && typeFile != ".PNG" && typeFile != ".JPG" && typeFile != ".JPEG") { ModelState.AddModelError("pathFile", "File is not image"); return(View(model)); } } else { model.PhotoPath = ""; } if (!ModelState.IsValid) { return(View(model)); } try { if (model.EmployeeID == 0) { //var path = Path.Combine(Server.MapPath("~/Images"), fileName); //file.SaveAs(path); int supplierId = HumanResourceBLL.Employee_Add(model, file); return(RedirectToAction("Index")); } else { bool updateResult = HumanResourceBLL.Employee_Update(model, file); //set cookie //var nameCoockie = new HttpCookie("name"); //nameCoockie.Expires = DateTime.Now.AddDays(-1); //Response.Cookies.Add(nameCoockie); //Response.Cookies["name"].Value = AccountBLL.Account_GetEmployee(account).FirstName + " " + AccountBLL.Account_GetEmployee(account).LastName; //var photoPathCoockie = new HttpCookie("photoPath"); //photoPathCoockie.Expires = DateTime.Now.AddDays(-1); //Response.Cookies.Add(photoPathCoockie); //Response.Cookies["photoPath"].Value = AccountBLL.Account_GetEmployee(account).PhotoPath; return(RedirectToAction("Index")); } } catch (Exception ex) { ModelState.AddModelError("", ex.Message + ": " + ex.StackTrace); return(View()); } }
public ActionResult Input(Employee model, HttpPostedFileBase uploadPhoto, string method) { try { if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } //Check email try { if (!HumanResourceBLL.Employee_CheckEmail(model.EmployeeID, model.Email, method)) { ModelState.AddModelError("Email", "Email already exists"); } } catch (Exception e) { ModelState.AddModelError("", e.Message + ":" + e.StackTrace); } //Upload ảnh if (uploadPhoto != null && uploadPhoto.ContentLength > 0) { string filePath = Path.Combine(Server.MapPath("~/Images"), uploadPhoto.FileName); uploadPhoto.SaveAs(filePath); model.PhotoPath = "Images/" + uploadPhoto.FileName; } else if (model.PhotoPath == null) { model.PhotoPath = ""; } DateTime hireDate = DateTime.Today; if ((hireDate.Year - (model.BirthDate).Year) < 18) { ModelState.AddModelError("BirthDate", "You must be over 18 years old"); } if (!ModelState.IsValid) { if (model.EmployeeID == 0) { ViewBag.Title = "Add New Employee"; ViewBag.ConfirmButton = "Add"; ViewBag.Method = "add"; return(View(model)); } else { ViewBag.Title = "Edit Employee"; ViewBag.ConfirmButton = "Save"; ViewBag.Method = "update"; return(View(model)); } } //Đưa dữ liệu vào CSDL if (model.EmployeeID == 0) { int shipperID = HumanResourceBLL.Employee_Add(model); return(RedirectToAction("Index")); } else { bool rs = HumanResourceBLL.Employee_Update(model); return(RedirectToAction("Index")); } } catch (Exception e) { ModelState.AddModelError("", e.Message + ":" + e.StackTrace); return(View(model)); } }
public ActionResult Input(Employee model, HttpPostedFileBase file = null, string oldEmail = "") { //try //{ // //Kiểm tra tính hợp lệ if (model.Notes == null) { model.Notes = ""; } if (String.IsNullOrEmpty(model.Country)) { ModelState.AddModelError("errorAddr", "Vui lòng chọn quốc gia"); } if (String.IsNullOrEmpty(model.GroupName)) { ModelState.AddModelError("errorRole", "Vui lòng chọn quyền"); } if (!ModelState.IsValid) { return(View(model)); } var listRoles = model.GroupName.Split(','); string groupName = ""; foreach (var item in SelectListHelper.listRoles(false)) { foreach (var role in listRoles) { if (item.Value == role) { groupName += role + ","; } } } model.GroupName = groupName.Remove(groupName.LastIndexOf(',')); if (file != null) { string get = DateTime.Now.ToString("ddMMyyyhhmmss"); string fileExtension = Path.GetExtension(file.FileName); string fileName = get + fileExtension; string path = Path.Combine(Server.MapPath("~/Images"), fileName); model.PhotoPath = fileName; file.SaveAs(path); } if (!EncodeMD5.IsMD5(model.Password)) { model.Password = EncodeMD5.GetMD5(model.Password); } if (model.EmployeeID == 0) { if (file == null) { TempData["emptyFile"] = "Vui lòng chọn file"; return(View(model)); } else if (HumanResourceBLL.Check_Email(model.Email)) { TempData["emptyEmail"] = "Email đã tồn tại"; return(View(model)); } else { int employeeID = HumanResourceBLL.Employee_Add(model); return(RedirectToAction("Index")); } } else { if (HumanResourceBLL.Check_Email(model.Email) && (oldEmail != model.Email)) { TempData["emptyEmail"] = "Email đã tồn tại"; ViewBag.oldEmail = oldEmail; return(View(model)); } var getEmployee = HumanResourceBLL.Employee_Get(model.EmployeeID); if (file == null) { model.PhotoPath = getEmployee.PhotoPath; } bool updateResult = HumanResourceBLL.Employee_Update(model); return(RedirectToAction("Index")); } //} //catch (Exception e) //{ // ModelState.AddModelError("", e.Message + ":" + e.StackTrace); // return View(model); //} }
public ActionResult Input(Employee model, HttpPostedFileBase uploadPhoto) { try { //kiem tra tinh hop le if (string.IsNullOrEmpty(model.FirstName)) { ModelState.AddModelError("FirstName", "First Name is required"); } if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "Last Name is required"); } if (string.IsNullOrEmpty(model.Password)) { ModelState.AddModelError("Password", "Password is required"); } if (string.IsNullOrEmpty(model.City)) { ModelState.AddModelError("City", "City is required"); } if (string.IsNullOrEmpty(model.Country)) { ModelState.AddModelError("Country", "Country is required"); } if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", "Email is required"); } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.HomePhone)) { model.HomePhone = ""; } if (string.IsNullOrEmpty(model.Title)) { model.Title = ""; } if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } if (string.IsNullOrEmpty(model.PhotoPath)) { model.PhotoPath = ""; } if (!ConvertMD5.IsMD5(model.Password)) { model.Password = ConvertMD5.GetMD5(model.Password); } if (!ModelState.IsValid) { return(View(model)); } if (model.EmployeeID == 0) { if (uploadPhoto != null) { string filePath = Path.Combine(Server.MapPath("~/Images"), uploadPhoto.FileName); model.PhotoPath = uploadPhoto.FileName; uploadPhoto.SaveAs(filePath); int employeeID = HumanResourceBLL.Employee_Add(model); return(RedirectToAction("Index")); } else { model.PhotoPath = ""; int employeeID = HumanResourceBLL.Employee_Add(model); return(RedirectToAction("Index")); } } else { if (uploadPhoto != null) { string filePath = Path.Combine(Server.MapPath("~/Images"), uploadPhoto.FileName); model.PhotoPath = uploadPhoto.FileName; uploadPhoto.SaveAs(filePath); bool rs = HumanResourceBLL.Employee_Update(model); return(RedirectToAction("Index")); } else { Employee employee = HumanResourceBLL.Employee_Get(model.EmployeeID); model.PhotoPath = employee.PhotoPath; bool rs = HumanResourceBLL.Employee_Update(model); return(RedirectToAction("Index")); } } } catch (Exception ex) { ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace); return(View(model)); } }
public ActionResult Input(Employee data, HttpPostedFileBase files = null) { if (data.Country == "null") { ModelState.AddModelError("errorAddr", "Vui lòng chọn quốc gia"); } if (data.Notes == null) { data.Notes = ""; } if (data.Address == null) { data.Address = ""; } if (data.HomePhone == null) { data.HomePhone = ""; } if (data.City == null) { data.City = ""; } if (data.PhotoPath == null) { data.PhotoPath = ""; } if (!ModelState.IsValid) { return(View(data)); } if (string.IsNullOrEmpty(data.FirstName)) { ModelState.AddModelError("ErrorFirstName", "First name is required"); } if (string.IsNullOrEmpty(data.LastName)) { ModelState.AddModelError("ErrorLastName", "Last Name is required"); } if (string.IsNullOrEmpty(data.Password)) { ModelState.AddModelError("ErrorPassword", "Password is required"); } /*if (string.IsNullOrEmpty(data.Email)) * { * data.Email = ""; * } * if (string.IsNullOrEmpty(data.Address)) * { * data.Address = ""; * } * if (string.IsNullOrEmpty(data.Notes)) * { * data.Notes = ""; * } * if (string.IsNullOrEmpty(data.HomePhone)) * { * data.HomePhone = ""; * }*/ if (files != null) { string get = DateTime.Now.ToString("ddMMyyyhhmmss"); string fileExtension = Path.GetExtension(files.FileName); string fileName = get + fileExtension; string path = Path.Combine(Server.MapPath("~/Images"), fileName); data.PhotoPath = fileName; files.SaveAs(path); } if (!EncodeMD5.IsMD5(data.Password)) { data.Password = EncodeMD5.GetMD5(data.Password); } if (data.EmployeeID == 0) { bool IsEmailExist = AccountBLL.IsEmailExist(data.Email, data.EmployeeID); if (IsEmailExist) { ModelState.AddModelError("errorEmailDuplicate", "Email đã tồn tại. Vui lòng nhập email khác!"); return(View(data)); } else { int employeeID = HumanResourceBLL.Employee_Add(data); return(RedirectToAction("Index")); } } else { bool IsEmailExist = AccountBLL.IsEmailExist(data.Email, data.EmployeeID); if (IsEmailExist) { ModelState.AddModelError("errorEmailDuplicate", "Email đã tồn tại. Vui lòng nhập email khác!"); return(View(data)); } else { bool updateResult = HumanResourceBLL.Employee_Update(data); } } return(RedirectToAction("Index")); }