public ActionResult Add(tbl_SanPhams model, HttpPostedFileBase image) { Poco_Page_ThemSanPham item = new Poco_Page_ThemSanPham(); if (string.IsNullOrEmpty(model.MoTaDai)) { model.MoTaDai = string.Empty; } if (string.IsNullOrEmpty(model.MoTaNgan)) { model.MoTaNgan = string.Empty; } model.DaXoa = false; model.SoLanXem = 0; model.SoLanMua = 0; model.NgayNhap = DateTime.Now; using (ModelEntities ctx = new ModelEntities()) { ctx.tbl_SanPhams.Add(model); ctx.SaveChanges(); item.DanhSachLoaiSanPham = ctx.tbl_LoaiSanPhams.ToList(); item.DanhSachNhaCungCap = ctx.tbl_NhaSanXuats.ToList(); if (image != null && image.ContentLength > 0) { //Tao folder chua hinh string patch1 = Server.MapPath("~/image"); string tagetdir = Path.Combine(patch1, model.SanPhamID.ToString()); Directory.CreateDirectory(tagetdir); //Copy hinh string mainThumn = Path.Combine(tagetdir, "main_thumn.jpg"); image.SaveAs(mainThumn); } return(View(item)); } }
public ActionResult Edit(tbl_SanPhams item, HttpPostedFileBase image) { using (ModelEntities ctx = new ModelEntities()) { tbl_SanPhams itemFind = ctx.tbl_SanPhams.Where(p => p.SanPhamID == item.SanPhamID).FirstOrDefault(); if (itemFind == null) { return(RedirectToAction("Index")); } itemFind.TenSanPham = item.TenSanPham; itemFind.Gia = item.Gia; itemFind.MoTaNgan = item.MoTaNgan; itemFind.MoTaDai = item.MoTaDai; itemFind.NongDo = item.NongDo; itemFind.SoLuong = item.SoLuong; itemFind.TinhTrang = item.TinhTrang; itemFind.LoaiSanPhamID = item.LoaiSanPhamID; itemFind.NhaSanXuatID = item.NhaSanXuatID; if (image != null && image.ContentLength > 0) { //Tao folder chua hinh string patch1 = Server.MapPath("~/image"); string tagetdir = Path.Combine(patch1, item.SanPhamID.ToString()); Directory.CreateDirectory(tagetdir); //Copy hinh string mainThumn = Path.Combine(tagetdir, "main_thumn.jpg"); image.SaveAs(mainThumn); } ctx.SaveChanges(); return(RedirectToAction("Edit", new { id = item.SanPhamID })); } }
public ActionResult Detail(int?id) { if (!id.HasValue) { return(RedirectToAction("Index", "Home")); } using (ModelEntities ctx = new ModelEntities()) { tbl_SanPhams n = ctx.tbl_SanPhams.Where(p => p.SanPhamID == id).FirstOrDefault(); Poco_Detail detail = new Poco_Detail(); detail.ThongTinSanPham = new tbl_SanPhams(); detail.ThongTinSanPham.SanPhamID = n.SanPhamID; detail.ThongTinSanPham.TenSanPham = n.TenSanPham; detail.ThongTinSanPham.DungTich = n.DungTich; detail.ThongTinSanPham.NongDo = n.NongDo; detail.ThongTinSanPham.Gia = n.Gia; detail.ThongTinSanPham.SoLanMua = n.SoLanMua; detail.ThongTinSanPham.SoLanXem = n.SoLanXem; detail.ThongTinSanPham.SoLuong = n.SoLuong; detail.ThongTinSanPham.TinhTrang = n.TinhTrang; detail.ThongTinSanPham.DaXoa = n.DaXoa; detail.ThongTinSanPham.MoTaDai = n.MoTaDai; detail.ThongTinSanPham.MoTaNgan = n.MoTaNgan; detail.ThongTinSanPham.NgayNhap = n.NgayNhap; detail.ThongTinSanPham.LoaiSanPhamID = n.LoaiSanPhamID; detail.ThongTinSanPham.NhaSanXuatID = n.NhaSanXuatID; detail.TenNhaSanXuat = ctx.tbl_NhaSanXuats.Where(p => p.NhaSanXuatID == n.NhaSanXuatID).First().TenNhaSanXuat; detail.TenLoaiSanPham = ctx.tbl_LoaiSanPhams.Where(p => p.LoaiSanPhamID == n.LoaiSanPhamID).First().TenLoaiSanPham; return(View(detail)); } }
//[MultipleButton(Name = "action", Argument = "Delete")] public ActionResult Delete(int?id) { using (ModelEntities ctx = new ModelEntities()) { tbl_SanPhams itemFind = ctx.tbl_SanPhams.Where(p => p.SanPhamID == id).FirstOrDefault(); itemFind.DaXoa = true; ctx.SaveChanges(); return(RedirectToAction("Index")); } }
public ActionResult Checkout(tbl_NguoiSuDungs itemUser) { tbl_PhieuOrders ord = new tbl_PhieuOrders { NgayLapPhieu = DateTime.Now, NguoiSuDungID = CurrentContext.getCurrenUser().NguoiSuDungID, TongSoLuong = 0, TongTien = 0, DiaChi = null, SoDienThoai = null, TinhTrangGiaoHang = false, TinhTrangThanhToan = false, DaXoa = false }; using (ModelEntities ctx = new ModelEntities()) { decimal total = 0; int totalAmount = 0; foreach (CartItem item in CurrentContext.Cart().Items) { tbl_SanPhams pro = ctx.tbl_SanPhams.Where(p => p.SanPhamID == item.ProID).FirstOrDefault(); if (pro != null) { tbl_ChiTietOrders d = new tbl_ChiTietOrders { SanPhamID = item.ProID, SoLuong = item.Quantity, DonGia = (decimal)pro.Gia, ThanhTien = (decimal)(item.Quantity * pro.Gia) }; pro.SoLanMua += item.Quantity; pro.SoLuong -= item.Quantity; ctx.SaveChanges(); ord.tbl_ChiTietOrders.Add(d); totalAmount += d.SoLuong; total += d.ThanhTien; } } ord.TongSoLuong = totalAmount; ord.TongTien = total; ord.TinhTrangGiaoHang = false; ord.TinhTrangThanhToan = false; int curID = CurrentContext.getCurrenUser().NguoiSuDungID; var user = ctx.tbl_NguoiSuDungs.Where(p => p.NguoiSuDungID == curID).FirstOrDefault(); if (itemUser.DiaChi == null) { ord.DiaChi = user.DiaChi; } else { ord.DiaChi = itemUser.DiaChi; } if (itemUser.SoDienThoai == null) { ord.SoDienThoai = user.SoDienThoai; } else { ord.SoDienThoai = itemUser.SoDienThoai; } ctx.tbl_PhieuOrders.Add(ord); ctx.SaveChanges(); CurrentContext.Cart().Items.Clear(); return(RedirectToAction("Index", "Cart")); } }