public async Task <ActionResult> Create(HangHoaViewModel hangHoa, HttpPostedFileBase hinhAnh) { if (hinhAnh != null && hinhAnh.ContentLength > 0) { try { string path = Path.Combine(Server.MapPath("~/Content/client/product"), Path.GetFileName(hinhAnh.FileName)); hinhAnh.SaveAs(path); hangHoa.hinhAnh = hinhAnh.FileName; } catch (Exception ex) { ViewBag.Message = "ERROR:" + ex.Message.ToString(); } } else { hangHoa.hinhAnh = "default.png"; } try { await _hangHoaKhoBus.Create(hangHoa); SetAlert("Đã thêm sản phẩm thành công!!!", "success"); } catch { TempData["hangHoa"] = hangHoa; SetAlert("Đã xảy ra lỗi! Bạn hãy thêm lại", "error"); } return(RedirectToAction("Index")); }
public IActionResult Session() { //gán session HttpContext.Session.SetString("HoTen", "Nguyễn Văn tèo"); HttpContext.Session.SetInt32("NamSinh", 1980); var hh = new HangHoaViewModel { MaHh = 1, TenHh = "Bia Sài Gòn", DonGia = 15900, TenLoai = "Bia" }; HttpContext.Session.Set <HangHoaViewModel>("Bia", hh); return(View()); }
public async Task <ActionResult> Edit(int id, HangHoaViewModel hangHoa, HttpPostedFileBase hinhAnh) { if (hinhAnh != null && hinhAnh.ContentLength > 0) { try { string path = Path.Combine(Server.MapPath("~/Content/client/product"), Path.GetFileName(hinhAnh.FileName)); hinhAnh.SaveAs(path); hangHoa.hinhAnh = hinhAnh.FileName; } catch (Exception ex) { ViewBag.Message = "ERROR:" + ex.Message.ToString(); } } else { // hangHoa.hinhAnh = "default.png"; hangHoa.hinhAnh = hangHoa.checkImage; } //Get hàng hóa muốn update (find by ID) HangHoa edit = (HangHoa)await _hangHoaKhoBus.Find(id); if (edit == null) { return(HttpNotFound()); } else { // Access Update from Business try { await _hangHoaKhoBus.Update(hangHoa, edit); SetAlert("Đã cập nhật hàng hóa thành công!!!", "success"); } catch { TempData["hangHoa"] = hangHoa; SetAlert("Đã xảy ra lỗi! Bạn hãy cập nhật lại", "error"); } } return(RedirectToAction("Index")); }
public async Task Create(object model) { var hangHoa = new HangHoa(); HangHoaViewModel input = (HangHoaViewModel)model; hangHoa.TenHangHoa = input.tenHangHoa; hangHoa.ModelName = input.modelName; hangHoa.DonViTinh = input.donViTinh; hangHoa.MaLoaiHangHoa = input.maLoaiHangHoa; hangHoa.XuatXu = input.xuatXu; hangHoa.ThoiGianBaoHanh = input.thoiGianBaoHanh; hangHoa.MoTa = input.moTa; hangHoa.ThongSoKyThuat = input.thongSoKyThuat; hangHoa.HinhAnh = input.hinhAnh; hangHoa.TrangThai = true; await _hangHoaRepo.InsertAsync(hangHoa); }
public async Task Update(object inputModel, object editModel) { HangHoaViewModel input = (HangHoaViewModel)inputModel; HangHoa editHangHoa = (HangHoa)editModel; editHangHoa.TenHangHoa = input.tenHangHoa; editHangHoa.ModelName = input.modelName; editHangHoa.GiaBan = input.giaBan; editHangHoa.GiamGia = input.giamGia; editHangHoa.DonViTinh = input.donViTinh; editHangHoa.MoTa = input.moTa; editHangHoa.ThongSoKyThuat = input.thongSoKyThuat; editHangHoa.XuatXu = input.xuatXu; editHangHoa.ThoiGianBaoHanh = input.thoiGianBaoHanh; editHangHoa.HinhAnh = input.hinhAnh; editHangHoa.MaLoaiHangHoa = input.maLoaiHangHoa; editHangHoa.TrangThai = input.trangThai; await _hangHoaRepo.EditAsync(editHangHoa); }