public ActionResult Update(tblCustomer obj, HttpPostedFileBase FileUpload, string key = "", string customergroup = "", string customerstatus = "", string RePassword = "", string hidFinger1 = "", string hidFinger2 = "", int page = 1) { ViewBag.keyValue = key; ViewBag.customergroupValue = customergroup; ViewBag.customerstatusValue = customerstatus; ViewBag.PN = page; ViewBag.CustomerGroups = GetMenuList(); ViewBag.ControllerList = _tblAccessControllerService.GetAllActive(); ViewBag.LevelList = _tblAccessLevelService.GetAllActive(); //Kiểm tra var oldObj = _tblCustomerService.GetById(obj.CustomerID); if (oldObj == null) { ViewBag.Error = "Bản ghi không tồn tại"; return(View(obj)); } if (!ModelState.IsValid) { return(View(oldObj)); } var existed = _tblCustomerService.GetByCode_Id(obj.CustomerCode, obj.CustomerID.ToString()); if (existed != null) { ModelState.AddModelError("CustomerCode", "Mã khách hàng đã tồn tại"); return(View(oldObj)); } if (!string.IsNullOrWhiteSpace(obj.Password)) { if (obj.Password != RePassword) { ModelState.AddModelError("Password", "Vui lòng nhập lại đúng mật khẩu"); return(View(oldObj)); } oldObj.Password = CryptorEngine.Encrypt(obj.Password, true); } if (!string.IsNullOrWhiteSpace(obj.DevPass)) { var devpass = _tblCustomerService.GetByDevPass(obj.DevPass); if (devpass != null && devpass.CustomerID != oldObj.CustomerID) { ModelState.AddModelError("DevPass", "Mật khẩu đã tồn tại"); return(View(oldObj)); } } //Gán giá trị oldObj.Account = obj.Account; oldObj.Address = obj.Address; oldObj.CustomerCode = obj.CustomerCode; oldObj.CompartmentId = obj.CompartmentId; oldObj.CustomerGroupID = obj.CustomerGroupID; oldObj.CustomerName = obj.CustomerName; oldObj.Description = obj.Description; oldObj.EnableAccount = obj.EnableAccount; oldObj.IDNumber = obj.IDNumber; oldObj.Inactive = obj.Inactive; oldObj.Mobile = obj.Mobile; oldObj.SortOrder = obj.SortOrder; oldObj.AccessLevelID = obj.AccessLevelID; oldObj.Finger1 = !string.IsNullOrWhiteSpace(hidFinger1) ? hidFinger1 : ""; oldObj.Finger2 = !string.IsNullOrWhiteSpace(hidFinger2) ? hidFinger2 : ""; oldObj.UserIDofFinger = obj.UserIDofFinger; oldObj.DevPass = !string.IsNullOrEmpty(obj.DevPass) ? obj.DevPass : ""; if (FileUpload != null) { var extension = Path.GetExtension(FileUpload.FileName) ?? ""; var fileName = Path.GetFileName(string.Format("{0}{1}", StringUtil.RemoveSpecialCharactersVn(FileUpload.FileName.Replace(extension, "")).GetNormalizeString(), extension)); var url = ConfigurationManager.AppSettings["FileUploadAvatar"]; oldObj.Avatar = string.Format("{0}{1}", url, fileName); } //Thực hiện cập nhật var result = _tblCustomerService.Update(oldObj); if (result.isSuccess) { WriteLog.Write(result, GetCurrentUser.GetUser(), obj.CustomerID.ToString(), obj.CustomerCode, "tblCustomer", ConstField.AccessControlCode, ActionConfigO.Update); UploadFile(FileUpload); return(RedirectToAction("Index", new { page = page, key = key, customergroup = customergroup, customerstatus = customerstatus, selectedId = oldObj.CustomerID })); } else { ModelState.AddModelError("", result.Message); return(View(oldObj)); } }
public ActionResult Update(tblCustomer obj, HttpPostedFileBase FileUpload, string key = "", string customergroup = "", string customerstatus = "", string RePassword = "", int page = 1) { var dictonary = FunctionHelper.GetLocalizeDictionary("Home", "notification"); ViewBag.keyValue = key; ViewBag.customergroupValue = customergroup; ViewBag.customerstatusValue = customerstatus; ViewBag.PN = page; ViewBag.CustomerGroups = GetMenuList(); var systemconfig = _tblSystemConfigService.GetDefault(); ViewBag.IsCompartment = systemconfig != null ? systemconfig.isCompartment : true; //Kiểm tra var oldObj = _tblCustomerService.GetById(obj.CustomerID); if (oldObj == null) { ViewBag.Error = "Bản ghi không tồn tại"; return(View(obj)); } if (!ModelState.IsValid) { return(View(oldObj)); } if (string.IsNullOrWhiteSpace(obj.CustomerName)) { ModelState.AddModelError("CustomerName", dictonary["enter_customer_name"]); return(View(oldObj)); } if (string.IsNullOrWhiteSpace(obj.CustomerCode)) { ModelState.AddModelError("CustomerCode", dictonary["enter_customer_code"]); return(View(oldObj)); } var existed = _tblCustomerService.GetByCode_Id(obj.CustomerCode, obj.CustomerID.ToString()); if (existed != null) { ModelState.AddModelError("CustomerCode", dictonary["Customer_code_already_exists"]); return(View(oldObj)); } if (!string.IsNullOrWhiteSpace(obj.Password)) { if (obj.Password != RePassword) { ModelState.AddModelError("Password", dictonary["correct_password"]); return(View(oldObj)); } oldObj.Password = CryptorEngine.Encrypt(obj.Password, true); } //Gán giá trị oldObj.Account = obj.Account; oldObj.Address = obj.Address; oldObj.CustomerCode = obj.CustomerCode; oldObj.CompartmentId = !string.IsNullOrEmpty(obj.CompartmentId) ? obj.CompartmentId.Trim() : ""; oldObj.CustomerGroupID = obj.CustomerGroupID; oldObj.CustomerName = obj.CustomerName; oldObj.Description = obj.Description; oldObj.EnableAccount = obj.EnableAccount; oldObj.IDNumber = obj.IDNumber; oldObj.Inactive = obj.Inactive; oldObj.Mobile = obj.Mobile; oldObj.SortOrder = obj.SortOrder; if (!string.IsNullOrEmpty(oldObj.Address)) { oldObj.AddressUnsign = StringUtil.RemoveSpecialCharactersVn(oldObj.Address.ToLower()).Replace("-", " "); } else { oldObj.AddressUnsign = ""; } if (FileUpload != null) { var extension = Path.GetExtension(FileUpload.FileName) ?? ""; var fileName = Path.GetFileName(string.Format("{0}{1}", StringUtil.RemoveSpecialCharactersVn(FileUpload.FileName.Replace(extension, "")).GetNormalizeString(), extension)); var url = ConfigurationManager.AppSettings["FileUploadAvatar"]; oldObj.Avatar = string.Format("{0}{1}", url, fileName); } //Thực hiện cập nhật var result = _tblCustomerService.Update(oldObj); if (result.isSuccess) { WriteLog.Write(result, GetCurrentUser.GetUser(), obj.CustomerID.ToString(), obj.CustomerCode, "tblCustomer", ConstField.ParkingCode, ActionConfigO.Update); WriteLog.WriteLogFile(result, GetCurrentUser.GetUser(), obj.CustomerID.ToString(), obj.CustomerCode, "tblCustomer", ConstField.ParkingCode, ActionConfigO.Update); UploadFile(FileUpload); return(RedirectToAction("Index", new { page = page, key = key, customergroup = customergroup, customerstatus = customerstatus, selectedId = oldObj.CustomerID })); } else { ModelState.AddModelError("", result.Message); return(View(oldObj)); } }