public async Task <ActionResult> HeThongEditMatHang(MatHangChungModel model) { if (!ModelState.IsValid) { return(RedirectToRoute("HethongEditMHC", new { id = model.MatHangChungId })); } var _mathang = await db.MatHangChungs.FindAsync(model.MatHangChungId); try { if (_mathang != null) { _mathang.TenMatHang = model.TenMatHang ?? null; _mathang.GiaCa = (int?)model.GiaCa ?? null; if (model.MaMatHangChung != null && model.HeThongId != null) { _mathang.MaMatHangChung = model.HeThongId.ToString() + "-" + model.MaMatHangChung; } string sghid = model.strGianHangChung != null?model.strGianHangChung.Split('_')[0].ToString() : null; string mgh = model.strGianHangChung != null?model.strGianHangChung.Split('_')[1].ToString() : null; _mathang.GianHangChungId = sghid != null?Convert.ToInt32(sghid) : (int?)null; _mathang.MaGianHangChung = mgh ?? null; _mathang.LoaiHang = model.LoaiHang ?? null; _mathang.TrangThai = model.TrangThai ?? null; _mathang.PhanTramKM = model.PhanTramKM ?? null; _mathang.NgayBDKM = model.NgayBDKM ?? null; _mathang.NgayKTKM = model.NgayKTKM ?? null; _mathang.AnhDaiDien = model.AnhDaiDien ?? null; _mathang.MoTa = model.MoTa ?? null; string _dsanh = ""; if (model.indivanh1 != null && model.indivanh1 != "") { _dsanh += model.indivanh1 + ","; } if (model.indivanh2 != null && model.indivanh2 != "") { _dsanh += model.indivanh2 + ","; } if (model.indivanh3 != null && model.indivanh3 != "") { _dsanh += model.indivanh3 + ","; } if (model.indivanh4 != null && model.indivanh4 != "") { _dsanh += model.indivanh4 + ","; } if (_dsanh != "") { _dsanh = _dsanh.Remove(_dsanh.Length - 1); } //YourString = YourString.Remove(YourString.Length - 1); //_dsanh = _dsanh.Remove(_dsanh.Length - 1); _mathang.DSHinhAnh = _dsanh != "" ? _dsanh : null; db.Entry(_mathang).State = EntityState.Modified; await db.SaveChangesAsync(); } else { return(RedirectToRoute("AdminPanel")); } } catch (Exception ex) { ModelState.AddModelError("", "Có lỗi xảy ra khi sửa gian hàng"); return(View(model)); } TempData["Updated"] = "Cập nhật mặt hàng thành công"; return(RedirectToRoute("HethongEditMHC", new { id = model.MatHangChungId })); }
public async Task <ActionResult> HeThongAddNewMatHang(MatHangChungModel model) { if (!ModelState.IsValid) { TempData["Error"] = "Vui lòng kiểm tra lại thông tin mặt hàng."; return(RedirectToRoute("HethongAddNewMHC")); } string _mamhcc = model.HeThongId.ToString() + "-" + model.MaMatHangChung; var _mhcc = db.MatHangChungs.Where(x => x.MaMatHangChung == _mamhcc).FirstOrDefault(); if (_mhcc != null) { TempData["Error"] = "Mã mặt hàng đã tồn tại."; return(RedirectToRoute("HethongAddNewMHC")); } try { MatHangChung _newMhc = new MatHangChung(); _newMhc.TenMatHang = model.TenMatHang ?? null; _newMhc.DSHinhAnh = model.DsHinhAnh ?? null; _newMhc.GiaCa = (int?)model.GiaCa ?? null; if (model.MaMatHangChung != null && model.HeThongId != default(int)) { _newMhc.MaMatHangChung = model.HeThongId.ToString() + "-" + model.MaMatHangChung; ViewBag.luuMaMatHangChung = model.MaMatHangChung; } string sghid = model.strGianHangChung != null?model.strGianHangChung.Split('_')[0].ToString() : null; string mgh = model.strGianHangChung != null?model.strGianHangChung.Split('_')[1].ToString() : null; _newMhc.GianHangChungId = sghid != null?Convert.ToInt32(sghid) : (int?)null; ViewBag.luugianhang = model.strGianHangChung; _newMhc.MaGianHangChung = mgh ?? null; _newMhc.LoaiHang = model.LoaiHang ?? null; _newMhc.TrangThai = model.TrangThai ?? null; ViewBag.luuTrangThai = model.TrangThai; _newMhc.PhanTramKM = model.PhanTramKM ?? null; _newMhc.NgayBDKM = model.NgayBDKM ?? null; _newMhc.NgayKTKM = model.NgayKTKM ?? null; ViewBag.NgayBDKM = model.NgayBDKM ?? null; ViewBag.NgayKTKM = model.NgayKTKM ?? null; ViewBag.luuLoaiHang = model.LoaiHang ?? null; _newMhc.AnhDaiDien = model.AnhDaiDien ?? null; _newMhc.MoTa = model.MoTa ?? null; string _dsanh = ""; if (model.indivanh1 != null && model.indivanh1 != "") { _dsanh += model.indivanh1 + ","; } if (model.indivanh2 != null && model.indivanh2 != "") { _dsanh += model.indivanh2 + ","; } if (model.indivanh3 != null && model.indivanh3 != "") { _dsanh += model.indivanh3 + ","; } if (model.indivanh4 != null && model.indivanh4 != "") { _dsanh += model.indivanh4 + ","; } if (_dsanh != "") { _dsanh = _dsanh.Remove(_dsanh.Length - 1); } //YourString = YourString.Remove(YourString.Length - 1); //_dsanh = _dsanh.Remove(_dsanh.Length - 1); _newMhc.DSHinhAnh = _dsanh != "" ? _dsanh : null; db.MatHangChungs.Add(_newMhc); await db.SaveChangesAsync(); TempData["Updated"] = "Đã thêm mới mặt hàng " + model.TenMatHang ?? "" + " vào hệ thống"; } catch { TempData["Error"] = "Có lỗi xảy ra khi thêm mặt hàng hàng."; return(RedirectToRoute("HethongAddNewMHC")); } return(RedirectToRoute("HethongAddNewMHC")); }
public async Task <ActionResult> HeThongEditMatHang(int?id) { if (id == null || id == 0) { return(RedirectToRoute("AdminPanel")); } MatHangChung _mathang = await db.MatHangChungs.FindAsync(id); if (_mathang == null) { return(RedirectToRoute("AdminPanel")); } ViewBag.gianhang = ListGianHang(); ViewBag.trangthaihang = ListTrangThaiHang(); ViewBag.loaihang = ListLoaiHang(); List <string> danhsachanh = new List <string>(); if (_mathang.DSHinhAnh != null) { danhsachanh.AddRange(_mathang.DSHinhAnh.Split(',')); } if (danhsachanh.Count == 1) { danhsachanh.AddRange(new string[] { "", "", "" }); } if (danhsachanh.Count == 2) { danhsachanh.AddRange(new string[] { "", "" }); } if (danhsachanh.Count == 3) { danhsachanh.Add(""); } if (danhsachanh.Count == 0) { danhsachanh.AddRange(new string[] { "", "", "", "" }); } var arrayAnh = danhsachanh.ToArray(); var getMatHang = new MatHangChungModel() { MatHangChungId = _mathang.MatHangChungId, TenMatHang = _mathang.TenMatHang, AnhDaiDien = _mathang.AnhDaiDien, GiaCa = (int)_mathang.GiaCa, strGianHangChung = _mathang.GianHangChungId + "_" + _mathang.MaGianHangChung, LoaiHang = _mathang.LoaiHang, MoTa = _mathang.MoTa, NgayBDKM = _mathang.NgayBDKM, NgayKTKM = _mathang.NgayKTKM, PhanTramKM = _mathang.PhanTramKM, TrangThai = _mathang.TrangThai, MaMatHangChung = _mathang.MaMatHangChung != null?_mathang.MaMatHangChung.Split('-')[1] : "", HeThongId = (int)_mathang.GianHangChung.HeThongId, indivanh1 = arrayAnh[0], indivanh2 = arrayAnh[1], indivanh3 = arrayAnh[2], indivanh4 = arrayAnh[3], }; return(View(getMatHang)); }