public ActionResult Input(Employee model, HttpPostedFileBase uploadFile) { try { //TODO: Kiểm tra tính hợp lệ của dữ liệu if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "LastName expected!"); } if (string.IsNullOrEmpty(model.FirstName)) { ModelState.AddModelError("FirstName", "FirstName expected!"); } if (string.IsNullOrEmpty(model.Title)) { ModelState.AddModelError("Title", "Title expected!"); } if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", "Email expected!"); } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.Country)) { model.Country = ""; } if (string.IsNullOrEmpty(model.City)) { model.City = ""; } if (string.IsNullOrEmpty(model.HomePhone)) { model.HomePhone = ""; } if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } if (string.IsNullOrEmpty(model.PhotoPath)) { model.PhotoPath = ""; } if (string.IsNullOrEmpty(model.Password)) { model.Password = "******"; } //Upload ảnh if (uploadFile != null) { string folder = Server.MapPath("~/Images/uploads"); //Lấy phần mở rộng của file string extensionName = System.IO.Path.GetExtension(uploadFile.FileName); // Tạo tên file ngẫu nhiên string fileName = $"{DateTime.Now.Ticks}{Path.GetExtension(uploadFile.FileName)}"; string filePath = Path.Combine(folder, fileName); uploadFile.SaveAs(filePath); model.PhotoPath = fileName; } if (!ModelState.IsValid) { return(View(model)); } //Lưu dữ liệu vào DB if (model.EmployeeID == 0) { model.Password = Helper.EncodeMD5(model.Password); CatalogBLL.AddEmployee(model); } else { CatalogBLL.UpdateEmployee(model); } return(RedirectToAction("Index")); } catch (Exception ex) { ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace); return(View(model)); } }
public ActionResult Input(Employee model, string[] Roles, HttpPostedFileBase uploadFile) { if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "LastName Expected"); } if (string.IsNullOrEmpty(model.FirstName)) { ModelState.AddModelError("FirstName", "FirstName Expected"); } if (string.IsNullOrEmpty(model.Title)) { ModelState.AddModelError("Title", "Title Expected"); } DateTime birthDate = model.BirthDate; if (birthDate == null) { ModelState.AddModelError("BirthDate", ""); } DateTime hireDate = model.HireDate; if (hireDate == null) { ModelState.AddModelError("HireDate", ""); } if (Roles != null) { for (int i = 1; i < Roles.Length; i++) { model.Roles += "," + Roles[i]; } } else { model.Roles = ""; } if (string.IsNullOrEmpty(model.Country)) { model.Country = ""; } if (string.IsNullOrEmpty(model.Email)) { model.Email = ""; } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.City)) { model.City = ""; } if (string.IsNullOrEmpty(model.Country)) { model.Country = ""; } if (string.IsNullOrEmpty(model.HomePhone)) { model.HomePhone = ""; } if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } if (string.IsNullOrEmpty(model.PhotoPath)) { model.PhotoPath = ""; } //if (string.IsNullOrEmpty(model.Password)) // model.Password = model.Password; //upload ảnh if (uploadFile != null) { string folder = Server.MapPath("~/Images/Uploads"); string fileName = $"{DateTime.Now.Ticks}{Path.GetExtension(uploadFile.FileName)}"; // string fileName = Guid.NewGuid() + uploadFile.FileName; string filePath = Path.Combine(folder, fileName); uploadFile.SaveAs(filePath); model.PhotoPath = fileName; } if (!ModelState.IsValid) { ViewBag.Title = model.EmployeeID == 0 ? "Create new Employee" : "Edit Employee"; return(View(model)); } //Lưu vào DB //TODO: Lưu dữ liệu vao DB if (model.EmployeeID == 0) { CatalogBLL.AddEmployee(model); } else { CatalogBLL.UpdateEmployee(model); } return(RedirectToAction("Index")); }
public ActionResult Input(Employee model, string[] Roles, HttpPostedFileBase uploadFile) { if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "LastName Expected"); } if (string.IsNullOrEmpty(model.FirstName)) { ModelState.AddModelError("FirstName", "FirstName Expected"); } if (string.IsNullOrEmpty(model.Title)) { ModelState.AddModelError("Title", "Title Expected"); } if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", "Email Expected"); } if (model.EmployeeID == 0) { if (CatalogBLL.CheckExist(model.Email) == 1) { ModelState.AddModelError("Email", "Email Existed"); } } if (string.IsNullOrEmpty(model.Password)) { ModelState.AddModelError("Password", "Password Expected"); } if (Roles != null) { for (int i = 1; i < Roles.Length; i++) { model.Roles += "," + Roles[i]; } } else { model.Roles = WebUserRoles.ANONYMOUS; } DateTime birthDate = model.BirthDate; var datestring = "01/01/0001"; var time1 = "01/01/2002"; var time2 = "01/01/1900"; var datecurrent = DateTime.Now; DateTime date = DateTime.Parse(datestring, System.Globalization.CultureInfo.InvariantCulture); if (birthDate == date) { ModelState.AddModelError("BirthDate", "BirthDate Expected"); } DateTime hireDate = model.HireDate; if (hireDate == date) { ModelState.AddModelError("HireDate", "HireDate Expected"); } //birthdate từ 1990 đên 2002 if (DateTime.Compare(birthDate, DateTime.Parse(time2)) < 0) { ModelState.AddModelError("BirthDate", "BirthDate Expected"); } if (DateTime.Compare(birthDate, DateTime.Parse(time1)) > 0) { ModelState.AddModelError("BirthDate", "BirthDate Expected"); } if (DateTime.Compare(hireDate, datecurrent) > 0) { ModelState.AddModelError("hireDate", "BirthDate Expected"); } if (string.IsNullOrEmpty(model.Country)) { model.Country = ""; } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.City)) { model.City = ""; } if (string.IsNullOrEmpty(model.Country)) { model.Country = ""; } if (string.IsNullOrEmpty(model.HomePhone)) { model.HomePhone = ""; } if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } if (string.IsNullOrEmpty(model.PhotoPath)) { model.PhotoPath = ""; } //if (string.IsNullOrEmpty(model.Password)) // model.Password = model.Password; //upload ảnh if (uploadFile != null) { string folder = Server.MapPath("~/Images/Uploads"); string fileName = $"{DateTime.Now.Ticks}{Path.GetExtension(uploadFile.FileName)}"; // string fileName = Guid.NewGuid() + uploadFile.FileName; string filePath = Path.Combine(folder, fileName); uploadFile.SaveAs(filePath); model.PhotoPath = fileName; } if (!ModelState.IsValid) { ViewBag.Title = model.EmployeeID == 0 ? "Create new Employee" : "Edit Employee"; return(View(model)); } //Lưu vào DB //TODO: Lưu dữ liệu vao DB if (model.EmployeeID == 0) { CatalogBLL.AddEmployee(model); } else { CatalogBLL.UpdateEmployee(model); } return(RedirectToAction("Index")); }
public ActionResult Input(Employee model, HttpPostedFileBase uploadFile, string detail, string updateProfile) { // lam that nho them try catch if (string.IsNullOrEmpty(model.FirstName)) { ModelState.AddModelError("FirstName", "**"); } if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "**"); } if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", "**"); } if (string.IsNullOrEmpty(model.HomePhone)) { ModelState.AddModelError("HomePhone", "**"); } if ((model.BirthDate) == null) { ModelState.AddModelError("BirthDate", "**"); } if ((model.HireDate) == null) { ModelState.AddModelError("HireDate", "**"); } if (model.HireDate.Year < model.BirthDate.Year + 18) { ModelState.AddModelError("HireDate", "HireDate must be at least 18 years older than BirthDate"); } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.City)) { model.City = ""; } if (string.IsNullOrEmpty(model.Country)) { model.Country = ""; } if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } if (string.IsNullOrEmpty(model.PhotoPath)) { model.PhotoPath = ""; } if (string.IsNullOrEmpty(model.Password)) { // dung ham sinh pass random => Cần mã hóa MD5 model.Password = RandomString(8); } if (!ModelState.IsValid) { if (model.EmployeeID == 0) { ViewBag.Title = "Create new a Employee"; SetAlert("Add new Employee Fail , Check again!", "warning"); } else { ViewBag.Title = "Update a Employee"; SetAlert("Update Employee Fail , Check again!", "warning"); } return(View(model)); } if (uploadFile != null) { //Tạo folder để upload ảnh lên Server string folder = Server.MapPath("~/Images/Upload"); // string fileName = uploadFile.FileName; => dễ bị trùng khi client chọn lại // => Solution : Sinh fileName theo giây phút để tránh trùng lặp //c1 : Cách ghép chuổi fileName string fileName = $"{DateTime.Now.Ticks}{Path.GetExtension(uploadFile.FileName)}"; //cach 2 // string fileName1 = string.Format("{0}{1}" ,DateTime.Now.Ticks,Path.GetExtension(uploadFile.FileName)); string filePath = Path.Combine(folder, fileName); //Upload ảnh lên Server uploadFile.SaveAs(filePath); // return content|redirectoaction() // return Json (model, JsonRequestBehavior.AllowGet); //Gán dữ liệu cho PhotoPath model.PhotoPath = fileName; } if (model.EmployeeID == 0) { CatalogBLL.AddEmployee(model); SetAlert("Add new Employee success !", "success"); } else if (detail == "detail") { CatalogBLL.UpdateEmployee(model); SetAlert("Update avatar success !", "success"); return(RedirectToAction("Index", "Account", new { id = @model.EmployeeID })); } else if (updateProfile == "updateProfile") { CatalogBLL.UpdateEmployee(model); SetAlert("Update profile success !", "success"); return(RedirectToAction("Index", "Account", new { id = @model.EmployeeID })); } else { SetAlert("Update Employee success !", "success"); CatalogBLL.UpdateEmployee(model); } return(RedirectToAction("Index")); }
public ActionResult Input(Employee model, HttpPostedFileBase uploadFile) { try { //TODO: Kiểm tra tính hợp le của dự liệu được nhập if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "LastName expected"); } if (string.IsNullOrEmpty(model.FirstName)) { ModelState.AddModelError("FirstName", "FirstName expected"); } if (string.IsNullOrEmpty(model.Title)) { ModelState.AddModelError("Title", "Title expected"); } if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", "Email expected"); } if (string.IsNullOrEmpty(model.Address)) { ModelState.AddModelError("Address", "Address expected"); } if (string.IsNullOrEmpty(model.City)) { model.City = ""; } if (string.IsNullOrEmpty(model.Country)) { model.Country = ""; } if (string.IsNullOrEmpty(model.HomePhone)) { model.HomePhone = ""; } if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } if (string.IsNullOrEmpty(model.PhotoPath)) { model.PhotoPath = ""; } if (string.IsNullOrEmpty(model.Password)) { ModelState.AddModelError("Password", "Password expected"); } if (uploadFile != null) { string folder = Server.MapPath("../Images/uploads"); //string fileName = uploadfile.FileName; string fileName = $"{DateTime.Now.Ticks}{Path.GetExtension(uploadFile.FileName)}"; string filePath = Path.Combine(folder, fileName); uploadFile.SaveAs(filePath); model.PhotoPath = fileName; } if (!ModelState.IsValid) { ViewBag.Title = model.EmployeeID == 0 ? "Add new employee" : "Edit a employee"; return(View(model)); } //TODO: Lưu dữ liệu vào DB if (model.EmployeeID == 0) { CatalogBLL.AddEmployee(model); } else { CatalogBLL.UpdateEmployee(model); } return(RedirectToAction("Index")); } catch (Exception ex) { ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace); return(View(model)); } }
public IActionResult Input(EmployeePostRequest model, string id = "") { try { if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "Last name expected"); } if (string.IsNullOrEmpty(model.FirstName)) { ModelState.AddModelError("FirstName", "First name expected"); } if (string.IsNullOrEmpty(model.Title)) { ModelState.AddModelError("Title", "Title expected"); } if (model.BirthDate.Year < 1753 || model.BirthDate.Year > 9999) { ModelState.AddModelError("BirthDate", "BirthDate's year must be between 1753 and 9999"); } if (model.HireDate.Year < 1753 || model.HireDate.Year > 9999) { ModelState.AddModelError("HireDate", "HireDate's year must be between 1753 and 9999"); } if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", "Email expected"); } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.City)) { model.City = ""; } if (string.IsNullOrEmpty(model.Country)) { model.Country = ""; } if (string.IsNullOrEmpty(model.HomePhone)) { model.HomePhone = ""; } if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } string photoPath = UploadedFile(model); photoPath = string.IsNullOrEmpty(id) ? "" : string.IsNullOrEmpty(photoPath) ? CatalogBLL.GetEmployee(model.EmployeeID).PhotoPath : photoPath; Employee employee = new Employee { EmployeeID = model.EmployeeID, LastName = model.LastName, FirstName = model.FirstName, Title = model.Title, BirthDate = model.BirthDate, HireDate = model.HireDate, Email = model.Email, Address = model.Address, City = model.City, Country = model.Country, HomePhone = model.HomePhone, Notes = model.Notes, PhotoPath = photoPath, }; // TODO: Save input into DB if (model.EmployeeID == 0) { // Set default password & role for new employee employee.Password = _passwordHasher.Hash(LiteCommerce.Common.Constants.DefaultPassword); employee.Roles = WebUserRoles.SALEMAN; CatalogBLL.AddEmployee(employee); } else { CatalogBLL.UpdateEmployee(employee); } return(RedirectToAction("Index")); } catch (System.Exception ex) { _logger.LogError(ex.Message + ": " + ex.StackTrace); ViewData["HeaderTitle"] = string.IsNullOrEmpty(id) ? "Create new employee" : "Edit employee"; return(View(model)); } }