public ActionResult AddProductPicture(Product model, HttpPostedFileBase file) { Product product = db.Products.Find(model.ID); if (product != null) { if (file != null) { try { string random = Helpers.DateHelper.GetTimeStamp(); Images product_img = new Images(); product_img.TID = model.ID; product_img.ImageType = ImageType.Product; product_img.ContentType = file.ContentType; product_img.Time = DateTime.Now; string root = "~/ProductFile/" + product.ProductName + "/"; var phicyPath = HostingEnvironment.MapPath(root); Directory.CreateDirectory(phicyPath); file.SaveAs(phicyPath + random + file.FileName); product_img.Path = "/ProductFile/" + product.ProductName + "/" + random + file.FileName; db.Images.Add(product_img); db.SaveChanges(); return Redirect("/Business/ProductShow/" + product.ID); } catch (Exception ex) { log.Error(new LogContent("商品图片增加出错", HttpHelper.GetIPAddress()), ex); return Redirect("/Business/ProductMessage/" + product.BusinessID); } } else { return Redirect("/Business/ProductMessage/" + product.BusinessID); } } else { return Redirect("/Business/ProductMessage/" + product.BusinessID); } }
public ActionResult ProductDelete(int id) { Product product = new Product(); List<Images> image = new List<Images>(); product = db.Products.Find(id); if (product != null) { try { image = db.Images.Where(c => c.TID == id).ToList(); foreach (Images item in image) { var phicyPath = HostingEnvironment.MapPath(item.Path); System.IO.File.Delete(phicyPath); db.Images.Remove(item); db.SaveChanges(); } db.SaveChanges(); db.Products.Remove(product); db.SaveChanges(); return Content("ok"); } catch (Exception ex) { log.Error(new LogContent("删除商品信息出错", HttpHelper.GetIPAddress()), ex); return Content("err"); } } else { return Content("err"); } }
public ActionResult AddProduct(Product model, int id) { if (ModelState.IsValid) { try { model.BusinessID = id; model.Time = DateTime.Now; model.IsPass = false; db.Products.Add(model); db.SaveChanges(); return Redirect("/Business/ProductManager/" + id); } catch (Exception ex) { log.Error(new LogContent("增加商品出错", HttpHelper.GetIPAddress()), ex); } } else { ModelState.AddModelError("", "产品信息填写错误!"); } ViewBag.Bussiness = db.Businesses.Find(id); return View(); }