public ActionResult Input(Employee model, HttpPostedFileBase PhotoPath, string PhotoPathDraft) { try { // Check Value if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "LastName is required"); } if (string.IsNullOrEmpty(model.FirstName)) { ModelState.AddModelError("FirstName", "FirstName is required"); } if (string.IsNullOrEmpty(model.Title)) { ModelState.AddModelError("Title", "Title is required"); } if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "LastName is required"); } if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", "Email is required"); } // Kiem tra email tồn tại khi thêm mới if (!HumanResourceBLL.Employee_CheckEmail(model.Email, "")) { ModelState.AddModelError("Email", "Email is exist"); } if (string.IsNullOrEmpty(model.BirthDate.ToString())) { ModelState.AddModelError("BirthDate", "BirthDate is required"); } if (string.IsNullOrEmpty(model.HireDate.ToString())) { ModelState.AddModelError("HireDate", "HireDate is required"); } if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", "Email is required"); } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.City)) { model.City = ""; } if (string.IsNullOrEmpty(model.Country)) { ModelState.AddModelError("Country", "Country is required"); } if (string.IsNullOrEmpty(model.HomePhone)) { model.HomePhone = ""; } if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } if (string.IsNullOrEmpty(model.Password)) { ModelState.AddModelError("Password", "Password is required"); } if (!ModelState.IsValid) { ViewBag.Title = model.EmployeeID == 0 ? "Add New Employee" : "Edit Employee"; return(View(model)); } // Upload file //TODO :upload image if (PhotoPath != null) { string FileName = $"{DateTime.Now.Ticks}{Path.GetExtension(PhotoPath.FileName)}"; string path = Path.Combine(Server.MapPath("~/Images"), FileName); PhotoPath.SaveAs(path); model.PhotoPath = FileName; } if (model.EmployeeID == 0) { int employeeID = CatalogBLL.Employee_Add(model); } else { bool updateEmployee = CatalogBLL.Employee_Update(model); } return(RedirectToAction("Index")); } catch (Exception ex) { ModelState.AddModelError("", ex.Message + " : " + ex.StackTrace); return(View(model)); } }
public ActionResult Edit(Account model, HttpPostedFileBase uploadPhoto) { if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", "Email is required"); } if (string.IsNullOrEmpty(model.HomePhone)) { ModelState.AddModelError("HomePhone", "Phone is required"); } if (string.IsNullOrEmpty(model.Address)) { ModelState.AddModelError("Address", "Address is required"); } if (string.IsNullOrEmpty(model.Country)) { ModelState.AddModelError("Country", "Country is required"); } if (string.IsNullOrEmpty(model.City)) { ModelState.AddModelError("City", "City is required"); } if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } HttpCookie requestCookies = Request.Cookies["userInfo"]; model.AccountID = Convert.ToInt32(requestCookies["AccountID"]); string emailCookie = Convert.ToString(requestCookies["Email"]); if (!HumanResourceBLL.Employee_CheckEmail(model.AccountID, model.Email, "update") && (model.Email != emailCookie)) { ModelState.AddModelError("Email", "Email ready exist"); } //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; requestCookies.Values["PhotoPath"] = Convert.ToString(model.PhotoPath); Response.SetCookie(requestCookies); } else if (model.PhotoPath == null) { model.PhotoPath = Convert.ToString(requestCookies["PhotoPath"]); } DateTime hireDate = DateTime.Today; if ((hireDate.Year - (model.BirthDate).Year) < 18) { ModelState.AddModelError("BirthDate", "You must be over 18 years old"); } //Kiểm tra có tồn tại bất kỳ lỗi nào hay không if (!ModelState.IsValid) { return(View(model)); } try { bool rs = AccountBLL.Account_Update(model); requestCookies.Values["Email"] = model.Email; Response.SetCookie(requestCookies); return(RedirectToAction("Index")); } catch (Exception e) { ModelState.AddModelError("", e.Message + ":" + e.StackTrace); return(View(model)); } }
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 Edit(Employee model, HttpPostedFileBase uploadPhoto) { if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } WebUserData userData = User.GetUserData(); model.EmployeeID = Convert.ToInt32(userData.UserID); string emailCookie = userData.Email; if (!HumanResourceBLL.Employee_CheckEmail(model.EmployeeID, model.Email, "update") && (model.Email != emailCookie)) { ModelState.AddModelError("Email", "Email ready exist"); } //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 = userData.Photo; } DateTime hireDate = DateTime.Today; if ((hireDate.Year - (model.BirthDate).Year) < 18) { ModelState.AddModelError("BirthDate", "You must be over 18 years old"); } //Kiểm tra có tồn tại bất kỳ lỗi nào hay không if (!ModelState.IsValid) { return(View(model)); } try { WebUserData userDatas = User.GetUserData(); string role = userDatas.GroupName; bool rs = UserAccountBLL.Account_Update(model); if ((model.Email != userData.Email) || (model.PhotoPath != userData.Photo)) { // WebUserData cookieData = new WebUserData() { UserID = model.EmployeeID.ToString(), FullName = model.LastName + " " + model.FirstName, GroupName = role, SessionID = Session.SessionID, ClientIP = Request.UserHostAddress, Photo = model.PhotoPath, Email = model.Email }; FormsAuthentication.SetAuthCookie(cookieData.ToCookieString(), false); return(RedirectToAction("Index")); } return(RedirectToAction("Index")); } catch (Exception e) { ModelState.AddModelError("", e.Message + ":" + e.StackTrace); return(View(model)); } }