public async Task <ActionResult> Update(long id, NhaUpdatingViewModel model, NhaUpdatingViewModel _modelBK) { try { if (ModelState.IsValid) { Nha nha = await _repository.GetRepository <Nha>().ReadAsync(id); decimal giaThueBQ = 0; if (!string.IsNullOrEmpty(model.TongGiaThue) && !string.IsNullOrEmpty(model.TongDienTichSuDung)) { giaThueBQ = Convert.ToDecimal(model.TongGiaThue.Replace(",", "")) / Convert.ToDecimal(model.TongDienTichSuDung.Replace(",", "")); } nha.MatBangId = model.MatBangId; nha.QuanId = Convert.ToInt64(model.QuanId); nha.DuongId = Convert.ToInt64(model.DuongId); nha.SoNha = StringHelper.KillChars(model.SoNha); nha.TenToaNha = StringHelper.KillChars(model.TenToaNha); nha.MatTienTreoBien = string.IsNullOrEmpty(model.MatTienTreoBien) ? 0 : float.Parse(model.MatTienTreoBien.Replace(",", ""), CultureInfo.InvariantCulture.NumberFormat); nha.BeNgangLotLong = string.IsNullOrEmpty(model.BeNgangLotLong) ? 0 : float.Parse(model.BeNgangLotLong.Replace(",", ""), CultureInfo.InvariantCulture.NumberFormat); nha.DienTichDat = string.IsNullOrEmpty(model.DienTichDat) ? 0 : float.Parse(model.DienTichDat.Replace(",", ""), CultureInfo.InvariantCulture.NumberFormat); nha.DienTichDatSuDungTang1 = string.IsNullOrEmpty(model.DienTichDatSuDungTang1) ? 0 : float.Parse(model.DienTichDatSuDungTang1.Replace(",", ""), CultureInfo.InvariantCulture.NumberFormat); nha.SoTang = string.IsNullOrEmpty(model.SoTang) ? 0 : Convert.ToInt32(model.SoTang); nha.TongDienTichSuDung = string.IsNullOrEmpty(model.TongDienTichSuDung) ? 0 : float.Parse(model.TongDienTichSuDung.Replace(",", ""), CultureInfo.InvariantCulture.NumberFormat); nha.DiChungChu = model.DiChungChu == "1" ? true : false; nha.Ham = model.Ham == "1" ? true : false; nha.ThangMay = model.ThangMay == "1" ? true : false; nha.NoiThatKhachThueCuId = Convert.ToInt32(model.NoiThatKhachThueCuId); nha.DanhGiaPhuHopVoiId = model.DanhGiaPhuHopVoiId; nha.TongGiaThue = string.IsNullOrEmpty(model.TongGiaThue) ? 0 : Convert.ToDecimal(model.TongGiaThue.Replace(",", "")); nha.GiaThueBQ = giaThueBQ; //string.IsNullOrEmpty(model.GiaThueBQ) ? 0 : Convert.ToDecimal(model.GiaThueBQ); nha.TenNguoiLienHeVaiTro = model.TenNguoiLienHeVaiTro; nha.SoDienThoai = model.SoDienThoai; nha.NgayCNHenLienHeLai = string.IsNullOrEmpty(model.NgayCNHenLienHeLai) ? (DateTime?)null : DateTime.ParseExact(model.NgayCNHenLienHeLai.Replace("-", "/"), "dd/MM/yyyy", CultureInfo.InvariantCulture); nha.CapDoTheoDoiId = Convert.ToInt32(model.CapDoTheoDoiId); nha.ImageDescription1 = model.ImageDescription1; nha.ImageDescription2 = model.ImageDescription2; nha.ImageDescription3 = model.ImageDescription3; nha.ImageDescription4 = model.ImageDescription4; nha.GhiChu = model.GhiChu; int result = await _repository.GetRepository <Nha>().UpdateAsync(nha, AccountId); //So sánh để tìm ra nội dung thay đổi if (result > 0) { //string noiDungThayDoi = ""; //string strOld = ""; //string strNew = ""; ////Mặt bằng //foreach (var item in _modelBK.ListMatBangArr) //{ // strOld = strOld == "" ? item.FieldName : strOld + ", " + item.FieldName; //} //foreach (var item in nha.MatBangId.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) //{ // string name = _repository.GetRepository<MatBang>().GetAll().Where(t => t.Id == Convert.ToInt32(item)).Select(o => o.Name).ToString(); // strNew = strNew == "" ? name : strNew + ", " + name; //} //if (strNew != strOld) //{ // noiDungThayDoi = "Sửa mặt bằng từ: " + strOld + " thành: " + strNew; //} ////Quận //strOld = strNew = ""; //if (_modelBK.QuanId != nha.QuanId.ToString()) //{ // strOld = _repository.GetRepository<Quan>().GetAll().Where(t => t.Id == Convert.ToInt32(_modelBK.QuanId)).Select(o => o.Name).ToString(); // strNew = _repository.GetRepository<Quan>().GetAll().Where(t => t.Id == Convert.ToInt32(nha.QuanId)).Select(o => o.Name).ToString(); // noiDungThayDoi = noiDungThayDoi == "" ? "Sửa quận từ: " + strOld + " thành: " + strNew : noiDungThayDoi + "<br/>" + "Sửa quận từ: " + strOld + " thành: " + strNew; //} ////Đường //strOld = strNew = ""; //if (_modelBK.DuongId != nha.DuongId.ToString()) //{ // strOld = _repository.GetRepository<Duong>().GetAll().Where(t => t.Id == Convert.ToInt32(_modelBK.DuongId)).Select(o => o.Name).ToString(); // strNew = _repository.GetRepository<Duong>().GetAll().Where(t => t.Id == Convert.ToInt32(nha.DuongId)).Select(o => o.Name).ToString(); // noiDungThayDoi = noiDungThayDoi == "" ? "Sửa đường từ: " + strOld + " thành: " + strNew : noiDungThayDoi + "<br/>" + "Sửa đường từ: " + strOld + " thành: " + strNew; //} ////Số nhà //strOld = strNew = ""; //if (_modelBK.SoNha != nha.SoNha) //{ // noiDungThayDoi = noiDungThayDoi == "" ? "Sửa số nhà từ: " + _modelBK.SoNha + " thành: " + nha.SoNha : noiDungThayDoi + "<br/>" + "Sửa số nhà từ: " + _modelBK.SoNha + " thành: " + nha.SoNha; //} TempData["Success"] = "Cập nhật bài viết thành công!"; return(RedirectToAction("Index")); } else { ModelState.AddModelError(string.Empty, "Cập nhật bài viết không thành công! Vui lòng kiểm tra và thử lại!"); return(View(model)); } } else { //var quan = _repository.GetRepository<Quan>().GetAll().OrderBy(o => o.Name).ToList(); //ViewBag.QuanDropdownlist = new SelectList(quan, "Id", "Name", model.QuanId); //ViewBag.DuongDropdownlist = new SelectList(_repository.GetRepository<Duong>().GetAll(o => o.QuanId == model.QuanId).OrderBy(o => o.Name).ToList(), "Id", "Name", model.DuongId); ModelState.AddModelError(string.Empty, "Vui lòng nhập chính xác các thông tin!"); return(View(model)); } } catch (Exception ex) { throw; } }
public async Task <ActionResult> Update(long id) { Nha nha = await _repository.GetRepository <Nha>().ReadAsync(id); SetViewBag(false); //SetViewBag(); var matBang = _repository.GetRepository <MatBang>().GetAll(); var listMatBangArr = new List <MatBangItem>(); if (matBang.Any()) { foreach (var item in matBang) { listMatBangArr.Add(new MatBangItem { FieldKey = item.Id, FieldName = item.Name, IsSelected = false }); } } if (!string.IsNullOrEmpty(nha.MatBangId)) { string[] arrmatbangid = nha.MatBangId.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (arrmatbangid.Count() > 0) { for (var i = 0; i < arrmatbangid.Count(); i++) { foreach (var item in listMatBangArr.Where(w => w.FieldKey == Convert.ToInt64(arrmatbangid[i]))) { item.IsSelected = true; } } } } var danhGia = _repository.GetRepository <DanhGiaPhuHopVoi>().GetAll(); var listDanhGiaArr = new List <DanhGiaPhuHopVoiItem>(); if (danhGia.Any()) { foreach (var item in danhGia) { listDanhGiaArr.Add(new DanhGiaPhuHopVoiItem { FieldKey = item.Id, FieldName = item.Name, IsSelected = false }); } } if (!string.IsNullOrEmpty(nha.DanhGiaPhuHopVoiId)) { string[] arrdanhgiaid = nha.DanhGiaPhuHopVoiId.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (arrdanhgiaid.Count() > 0) { for (var i = 0; i < arrdanhgiaid.Count(); i++) { foreach (var item in listDanhGiaArr.Where(w => w.FieldKey == Convert.ToInt32(arrdanhgiaid[i]))) { item.IsSelected = true; } } } } NhaUpdatingViewModel model = new NhaUpdatingViewModel() { Id = nha.Id, SoNha = nha.SoNha, //MatBangId = nha.MatBangId, ListMatBangArr = listMatBangArr, ListDanhGiaPhuHopVoiArr = listDanhGiaArr, QuanId = nha.QuanId.ToString(), DuongId = nha.DuongId.ToString(), TenToaNha = nha.TenToaNha, NoiThatKhachThueCuId = nha.NoiThatKhachThueCuId.ToString(), //DanhGiaPhuHopVoiId = nha.DanhGiaPhuHopVoiId.ToString(), CapDoTheoDoiId = nha.CapDoTheoDoiId.ToString(), MatTienTreoBien = nha.MatTienTreoBien.ToString(), BeNgangLotLong = nha.BeNgangLotLong.ToString(), DienTichDat = nha.DienTichDat.ToString(), DienTichDatSuDungTang1 = nha.DienTichDatSuDungTang1.ToString(), SoTang = nha.SoTang.ToString(), TongDienTichSuDung = nha.TongDienTichSuDung.ToString(), DiChungChu = nha.DiChungChu ? "1" : "0", Ham = nha.Ham ? "1" : "0", ThangMay = nha.ThangMay ? "1" : "0", TongGiaThue = nha.TongGiaThue.ToString(), GiaThueBQ = nha.GiaThueBQ.ToString(), TenNguoiLienHeVaiTro = nha.TenNguoiLienHeVaiTro, SoDienThoai = nha.SoDienThoai, NgayCNHenLienHeLai = nha.NgayCNHenLienHeLai.HasValue ? nha.NgayCNHenLienHeLai.Value.ToString("dd/MM/yyyy") : "", ImageDescription1 = nha.ImageDescription1, ImageDescription2 = nha.ImageDescription2, ImageDescription3 = nha.ImageDescription3, ImageDescription4 = nha.ImageDescription4, GhiChu = nha.GhiChu, }; //Backup model để so sánh xem có update gì không, sau đó lưu vào lịch sử _modelBK = model; return(View(model)); }