public async Task <ActionResult> Read(long id, AccountUpdatingViewModel model) { if (ModelState.IsValid) { try { string email = StringHelper.KillChars(model.Email); //Kiểm tra trùng email bool result = await _repository.GetRepository <Account>().AnyAsync(o => o.Email == email && o.Id != id); if (result) { return(Json(new { success = false, message = "Địa chi email đã được sử dụng. Vui lòng nhập địa chỉ email khác!" }, JsonRequestBehavior.AllowGet)); } Account account = await _repository.GetRepository <Account>().ReadAsync(id); if (account == null) { return(Json(new { success = false, message = "Không tìm thấy tài khoản người dùng!" }, JsonRequestBehavior.AllowGet)); } //Cập nhật thông tin account.Name = StringHelper.KillChars(model.Name); account.Email = email; account.PhoneNumber = StringHelper.KillChars(model.PhoneNumber); account.ProfilePicture = StringHelper.KillChars(model.ProfilePicture);; int updateResult = await _repository.GetRepository <Account>().UpdateAsync(account, AccountId); if (updateResult > 0) { if (id == AccountId) { Session["Email"] = account.Email; Session["AccountId"] = account.Id; Session["AccountName"] = account.Name; Session["ProfilePicture"] = account.ProfilePicture; } return(Json(new { success = true, message = "Cập nhật thông tin tài khoản thành công!" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { success = false, message = "Cập nhật thông tin tài khoản không thành công!" }, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { return(Json(new { success = false, message = "Đã xảy ra lỗi: " + ex.Message }, JsonRequestBehavior.AllowGet)); } } else { return(Json(new { success = false, message = "Vui lòng nhập chính xác các thông tin!" }, JsonRequestBehavior.AllowGet)); } }
public async Task <ActionResult> Read(long id, AccountUpdatingViewModel model, HttpPostedFileBase file) { if (ModelState.IsValid) { try { //Upload string photo = ""; try { string fullFilePath = "/Uploads/images/anh-nguoi-dung/"; string filePath = Server.MapPath("~/Uploads/images/anh-nguoi-dung/"); try { if (!Directory.Exists(filePath)) { DirectoryInfo di = Directory.CreateDirectory(filePath); } } catch { } if (file != null && file.ContentLength > 0) { string fileName = CommonHelper.ToURL(Path.GetFileNameWithoutExtension(file.FileName), 0) + Path.GetExtension(file.FileName); var path = Path.Combine(filePath, fileName); if (System.IO.File.Exists(path)) { fileName = string.Format("{0}_{1}{2}", CommonHelper.ToURL(Path.GetFileNameWithoutExtension(file.FileName), 0), String.Format("{0:dd_MM_yyyy_hh_mm_ss_fff}", DateTime.Now), Path.GetExtension(file.FileName)); path = Path.Combine(filePath, fileName); } file.SaveAs(path); fullFilePath = fullFilePath + fileName; photo = fullFilePath; } } catch { } string email = StringHelper.KillChars(model.Email); //Kiểm tra trùng email bool result = await _repository.GetRepository <Account>().AnyAsync(o => o.Email == email && o.Id != id); if (result) { return(Json(new { success = false, message = "Địa chi email đã được sử dụng. Vui lòng nhập địa chỉ email khác!" }, JsonRequestBehavior.AllowGet)); } Account account = await _repository.GetRepository <Account>().ReadAsync(id); if (account == null) { return(Json(new { success = false, message = "Không tìm thấy tài khoản người dùng!" }, JsonRequestBehavior.AllowGet)); } //Cập nhật thông tin string dob = StringHelper.KillChars(model.DateOfBirth); if (!string.IsNullOrEmpty(dob)) { try { DateTime date = DateTime.ParseExact(dob, "dd/MM/yyyy", null); account.DateOfBirth = date; } catch { account.DateOfBirth = null; } } account.FullName = StringHelper.KillChars(model.FullName); //account.Code = StringHelper.KillChars(model.Code); account.Email = email; account.PhoneNumber = StringHelper.KillChars(model.PhoneNumber); //account.IsExpertsAccount = model.IsExpertsAccount; account.Sex = model.Sex; account.Address = StringHelper.KillChars(model.Address); account.IdDonVi = model.IdDonVi; if (!string.IsNullOrEmpty(photo)) { account.ProfilePicture = photo;//StringHelper.KillChars(model.ProfilePicture); ; } int updateResult = await _repository.GetRepository <Account>().UpdateAsync(account, AccountId); if (updateResult > 0) { if (id == AccountId) { Session["Email"] = account.Email; Session["AccountId"] = account.Id; Session["AccountName"] = account.FullName; Session["ProfilePicture"] = account.ProfilePicture; Session["IsExpertsAccount"] = account.IsExpertsAccount; Session["IsManageAccount"] = account.IsManageAccount; Session[SessionEnum.AccountDv] = account.IdDonVi.ToString(); } return(Json(new { success = true, message = "Cập nhật thông tin tài khoản thành công!" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { success = false, message = "Cập nhật thông tin tài khoản không thành công!" }, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { return(Json(new { success = false, message = "Đã xảy ra lỗi: " + ex.Message }, JsonRequestBehavior.AllowGet)); } } else { return(Json(new { success = false, message = "Vui lòng nhập chính xác các thông tin!" }, JsonRequestBehavior.AllowGet)); } }