public ActionResult 修改產品(int id) { MotaiDataEntities db = new MotaiDataEntities(); tProduct product = db.tProducts.FirstOrDefault(p => p.ProductId == id); if (product == null) { return(RedirectToAction("員工看產品頁面")); } EmpProductViewModel Prod = new EmpProductViewModel(); Prod.ProductId = id; Prod.pNumber = product.pNumber; Prod.pName = product.pName; Prod.psCategory = product.tProductCategory.Category; Prod.psMaterial = product.tProductMaterial.Material; Prod.psSize = product.tProductSize.Size; Prod.pLxWxH = product.pLxWxH; Prod.pWeight = product.pWeight; Prod.pIntroduction = product.pIntroduction; Prod.pPrice = product.pPrice; var categories = new ProductRespoitory().GetCategoryAll(); List <SelectListItem> Cateitems = new ProductRespoitory().GetSelectList(categories); Prod.Categories = Cateitems; var materials = new ProductRespoitory().GetMaterialAll(); List <SelectListItem> Mateitems = new ProductRespoitory().GetSelectList(materials); Prod.Materials = Mateitems; var sizes = new ProductRespoitory().GetSizeAll(); List <SelectListItem> Sizeitems = new ProductRespoitory().GetSelectList(sizes); Prod.Sizes = Sizeitems; return(View(Prod)); }
public ActionResult 修改產品(EmpProductViewModel p) { if (Session[CSession關鍵字.SK_LOGINED_EMPLOYEE] == null) { return(RedirectToAction("員工登入", "Employee")); } MotaiDataEntities dbContext = new MotaiDataEntities(); tProduct prod = dbContext.tProducts.Find(p.ProductId); if (prod != null) { prod.pNumber = p.pNumber; prod.pName = p.pName; prod.pCategory = p.pCategory; prod.pMaterial = p.pMaterial; prod.pSize = p.pSize; prod.pLxWxH = p.pLxWxH; prod.pWeight = p.pWeight; prod.pPrice = p.pPrice; List <tProductImage> oldImages = dbContext.tProductImages.Where(imgs => imgs.ProductId.Equals(p.ProductId)).ToList(); if (oldImages.Count > p.pImage.Count) { int index = 0; foreach (var oldItem in oldImages) { if (index < p.pImage.Count) { if (p.pImage[index] == null) { break; } if (p.pImage[index].ContentLength > 0) { FileInfo file = new FileInfo(p.pImage[index].FileName); string photoName = Guid.NewGuid().ToString() + file.Extension; p.pImage[index].SaveAs(Server.MapPath("~/images/" + photoName)); oldItem.pImage = Url.Content("~/images/" + photoName); //Directory.Delete(Url.Content(oldItem.pImage)); } } else { dbContext.tProductImages.Remove(oldItem); } index++; } } else { int index = 0; foreach (var item in p.pImage) { if (index < oldImages.Count) { FileInfo file = new FileInfo(item.FileName); string photoName = Guid.NewGuid().ToString() + file.Extension; item.SaveAs(Server.MapPath("~/images/" + photoName)); oldImages[index].pImage = Url.Content("~/images/" + photoName); } else { tProductImage image = new tProductImage(); FileInfo file = new FileInfo(item.FileName); string photoName = Guid.NewGuid().ToString() + file.Extension; item.SaveAs(Server.MapPath("~/images/" + photoName)); image.ProductId = p.ProductId; image.pImage = "~" + Url.Content("~/images/" + photoName); dbContext.tProductImages.Add(image); } index++; } } dbContext.SaveChanges(); } return(RedirectToAction("倉儲看產品頁面")); }