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)); } }
// GET: Admin/Products public ActionResult Add() { Poco_Page_ThemSanPham item = new Poco_Page_ThemSanPham(); using (ModelEntities ctx = new ModelEntities()) { item.DanhSachLoaiSanPham = ctx.tbl_LoaiSanPhams.ToList(); item.DanhSachNhaCungCap = ctx.tbl_NhaSanXuats.ToList(); return(View(item)); } }