public ActionResult addProduct(Product model, HttpPostedFileBase fuMain, HttpPostedFileBase fuThumbs) { using (QLBHEntities ctx = new QLBHEntities()) { if (string.IsNullOrEmpty(model.TinyDes)) model.TinyDes = string.Empty; if (string.IsNullOrEmpty(model.FullDes)) model.FullDes = string.Empty; ctx.Products.Add(model); ctx.SaveChanges(); var list = ctx.Categories.ToList(); var list2 = ctx.TypeProducts.ToList(); ViewBag.TypeProduct = list2; ViewBag.Categories = list; } if (fuMain != null && fuMain.ContentLength > 0 && fuThumbs != null && fuThumbs.ContentLength > 0) { //tao folder chua hinh [~/Imgs/sp/{ID}] string spDirPath = Server.MapPath("~/Imgs/sp"); string targetDirPath = Path.Combine(spDirPath, model.ProID.ToString()); Directory.CreateDirectory(targetDirPath); //copy hinh len string mainFileName = Path.Combine(targetDirPath, "main.jpg"); fuMain.SaveAs(mainFileName); string thumbsFileName = Path.Combine(targetDirPath, "main_thumbs.jpg"); fuThumbs.SaveAs(thumbsFileName); } ViewBag.Added = true; return View(); }
public ActionResult editProduct(Product model, HttpPostedFileBase fuMain, HttpPostedFileBase fuThumbs) { Product pro = new Product(); using (QLBHEntities ctx = new QLBHEntities()) { pro = ctx.Products.Where(p => p.ProID == model.ProID).First(); if (string.IsNullOrEmpty(model.TinyDes)) model.TinyDes = string.Empty; if (string.IsNullOrEmpty(model.FullDes)) model.FullDes = string.Empty; pro.CatID = model.CatID; pro.TypeID = model.TypeID; pro.ProName = model.ProName; pro.Quantity = model.Quantity; pro.Price = model.Price; pro.Origin = model.Origin; pro.TinyDes = model.TinyDes; pro.FullDes = model.FullDes; ctx.SaveChanges(); var list = ctx.Categories.ToList(); var list2 = ctx.TypeProducts.ToList(); ViewBag.TypeProduct = list2; ViewBag.Categories = list; } if (fuMain != null && fuMain.ContentLength > 0 && fuThumbs != null && fuThumbs.ContentLength > 0) { //tao folder chua hinh [~/Imgs/sp/{ID}] string spDirPath = Server.MapPath("~/Imgs/sp"); string targetDirPath = Path.Combine(spDirPath, model.ProID.ToString()); Directory.CreateDirectory(targetDirPath); //copy hinh len string mainFileName = Path.Combine(targetDirPath, "main.jpg"); fuMain.SaveAs(mainFileName); string thumbsFileName = Path.Combine(targetDirPath, "main_thumbs.jpg"); fuThumbs.SaveAs(thumbsFileName); } ViewBag.Added = true; return View(pro); }