/// <summary> /// 添加一条记录 /// </summary> public ResultSet Add(ProductImg entity) { Func <ProductImg, ResultStatus> validate = (_entity) => { return(new ResultStatus()); }; Func <ProductImg, ResultStatus> op = (_entity) => { int ret = new ProductImgDal().Add(entity); if (ret > 0) { return(new ResultStatus()); } else { return new ResultStatus() { Success = false, Code = StatusCollection.AddFailed.Code, Description = StatusCollection.AddFailed.Description } }; }; return(HandleBusiness(entity, op, validate)); }
public IActionResult CreatProductImage(IFormFile imagename) { string imgname = ""; int id = int.Parse(TempData["ProductImage"].ToString()); if (imagename != null) { if (ImageSecurity.ImageValidator(imagename)) { imgname = imagename.SaveImage("", "wwwroot/Layout/img/Gallery"); string olldpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Layout/img/Gallery", imgname); string newpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Layout/img/Prodoctsubimg", imgname); olldpath.Image_resize(newpath, 150, 150); ProductImg productImg = new ProductImg { ProdoctImgName = imgname, ProductId = id }; GalleryService.AddProductImage(productImg); } else { return(RedirectToAction(nameof(GalleryList), new { id = id })); } } return(RedirectToAction(nameof(GalleryList), new { id = id })); }
//[ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "Id,Pid,Pimg")] ProductImg productImg, HttpPostedFileBase Pimg) { if (ModelState.IsValid) { if (Pimg != null) { if (Pimg.ContentType.IndexOf("image", System.StringComparison.Ordinal) == -1) { ViewBag.Message = "檔案型態錯誤!"; return(View()); } //取得副檔名 string extension = Path.GetExtension(Pimg.FileName); //新檔案名稱 string fileName = String.Format("{0:yyyyMMddhhmmsss}{1}", DateTime.Now, extension); string savedName = Path.Combine(Server.MapPath("/Img/product"), fileName); Pimg.SaveAs(savedName); productImg.Pimg = fileName; } //db.Entry(productImg).State = EntityState.Modified; db.ProductImg.Add(productImg); db.SaveChanges(); var newProductImg = db.ProductImg.Where(x => x.Pid == productImg.Pid).ToList(); return(RedirectToAction("Edit", "BKProductImgs", new { id = productImg.Pid })); } //ViewBag.Pid = new SelectList(db.ProductLists, "Id", "Name", productImg.Pid); return(RedirectToAction("Edit", "BKProductImgs", new { id = productImg.Pid })); }
public ActionResult Delete(int id) { ProductImg productImg = db.ProductImg.Find(id); db.ProductImg.Remove(productImg); db.SaveChanges(); return(RedirectToAction("Edit", "BKProductImgs", new { id = productImg.Pid })); }
public bool AddProductImage(ProductImg productImg) { try { dB.Add(productImg); dB.SaveChanges(); return(true); } catch { return(false); } }
/// <summary> /// 添加一条记录 /// </summary> public int Add(ProductImg entity, IDbTransaction tran) { string sql = @"insert into [ProductImg] ([imgUrl], [productId], [orderId]) values (@imgUrl, @productId, @orderId)"; object param = new { imgUrl = entity.ImgUrl, productId = entity.ProductId, orderId = entity.OrderId }; int count = tran.Connection.Execute(sql, param, tran); return(count); }
public ActionResult Check(int id) { int count = db.ProductImg.Where(x => x.Pid == id).Count(); if (count > 0) { return(RedirectToAction("Index", "BKProduct")); } else { ProductImg productImg = new ProductImg(); productImg.Pid = id; productImg.Pimg = "0.jpg"; db.ProductImg.Add(productImg); db.SaveChanges(); return(RedirectToAction("Index", "BKProduct")); } }
public int Update(ProductImg entity) { //GetUpdateSql2 string sql = @"update [ProductImg] set imgUrl=@imgUrl, productId=@productId, orderId=@orderId where id=@id "; object param = new { id = entity.Id, imgUrl = entity.ImgUrl, productId = entity.ProductId, orderId = entity.OrderId }; using (IDbConnection conn = OpenConnection()) { int count = conn.Execute(sql, param); return(count); } }
public ActionResult Upload(int id, HttpPostedFileBase img) { if (img == null) { return(Content("Error")); } var pimg = new ProductImg { ProductID = id, ImgMimeType = img.ContentType, ImgData = new byte[img.ContentLength] }; img.InputStream.Read(pimg.ImgData, 0, img.ContentLength); _context.ProductImg.Add(pimg); _context.SaveChanges(); return(Content("OK")); }
/// <summary> /// 添加一条记录 /// </summary> public int Add(ProductImg entity) { string sql = @"insert into [ProductImg] ([imgUrl], [productId], [orderId]) values (@imgUrl, @productId, @orderId)"; object param = new { imgUrl = entity.ImgUrl, productId = entity.ProductId, orderId = entity.OrderId }; using (IDbConnection conn = OpenConnection()) { int count = conn.Execute(sql, param); return(count); } }
protected void btnAddCategory_Click(object sender, EventArgs e) { ProductDbContext productDbContext = new ProductDbContext(); ProductLayer productLayer = new ProductLayer(); Product product = new Product(); productLayer.CategoryID = Convert.ToInt64(txtCategoryID.Text); productLayer.Name = txtProductName.Text; productLayer.Price = Convert.ToDouble(txtPrice.Text); productLayer.Details = txtProductDetails.Text; productLayer.Brand = txtBrandName.Text; productLayer.StockQuantity = Convert.ToDouble(txtStockQuentity.Text); productLayer.IsFavorite = chkIsFavourite.Checked; productLayer.IsActive = chkIsActive.Checked; product.CategoryID = productLayer.CategoryID; product.Name = productLayer.Name; product.Price = productLayer.Price; product.Details = productLayer.Details; product.Brand = productLayer.Brand; product.StockQuantity = productLayer.StockQuantity; product.IsFavorite = productLayer.IsFavorite; product.IsActive = productLayer.IsActive; productDbContext.Products.Add(product); productDbContext.SaveChanges(); long ProductID = product.ID; ProductImg productImg = new ProductImg(); ProductImgLayer productImgLayer = new ProductImgLayer(); int displayOrder = 1; string activePictureName = uploadActivePicture.FileName; foreach (var a in uploadProductPicture.PostedFiles) { string fileExtension = Path.GetExtension(a.FileName); if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".png") { Random random = new Random(); int randomNum = random.Next(0, 100000000); a.SaveAs(Server.MapPath("~/Photos/Products/" + randomNum + a.FileName)); string imagePath = "~/Photos/Products/" + randomNum + a.FileName; if (activePictureName == a.FileName) { productImgLayer.IsDefaultImg = true; } else { productImgLayer.IsDefaultImg = false; } productImgLayer.ProductID = ProductID; productImgLayer.ImgPath = imagePath; productImgLayer.DisplayOrder = displayOrder; productImg.ProductID = productImgLayer.ProductID; productImg.ImgPath = productImgLayer.ImgPath; productImg.DisplayOrder = productImgLayer.DisplayOrder; productImg.IsDefaultImg = productImgLayer.IsDefaultImg; productDbContext.ProductImgs.Add(productImg); productDbContext.SaveChanges(); displayOrder += 1; } } GridView1.DataBind(); ClearProductInputField(); }
public IActionResult Upsert(ProductVM productVM) { if (ModelState.IsValid) { string webRootPath = _hostEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; List <ProductImg> prodImgs = new List <ProductImg>(); if (files.Count > 0) { for (int i = 0; i < files.Count; i++) { string fileName = Guid.NewGuid().ToString(); var uploads = Path.Combine(webRootPath, @"images\products"); var extenstion = Path.GetExtension(files[i].FileName); if (productVM.Product.Id != 0) { Product objProduct = _unitOfWork.Product.GetProductWithImgOnId(productVM.Product.Id); //this is an edit and we need to remove old image foreach (var item in objProduct.ImgDetails) { var imagePath = Path.Combine(webRootPath, item.ImageUrl.TrimStart('\\')); if (System.IO.File.Exists(imagePath)) { System.IO.File.Delete(imagePath); } } } using (var filesStreams = new FileStream(Path.Combine(uploads, fileName + extenstion), FileMode.Create)) { files[i].CopyTo(filesStreams); } ProductImg prodImg = new ProductImg() { ImageUrl = @"\images\products\" + fileName + extenstion }; prodImgs.Add(prodImg); } productVM.Product.ImgDetails = prodImgs; } else { //update when they do not change the image if (productVM.Product.Id != 0) { Product objFromDb = _unitOfWork.Product.GetProductWithImgOnId(productVM.Product.Id); foreach (var item in objFromDb.ImgDetails) { ProductImg prodImg = new ProductImg() { ImageUrl = item.ImageUrl }; prodImgs.Add(prodImg); } productVM.Product.ImgDetails = prodImgs; } } if (productVM.Product.Id == 0) { _unitOfWork.Product.Add(productVM.Product); } else { _unitOfWork.Product.Update(productVM.Product); } _unitOfWork.Save(); return(RedirectToAction(nameof(Index))); } else { productVM.CategoryList = _unitOfWork.Category.GetAll().Select(i => new SelectListItem { Text = i.Name, Value = i.Id.ToString() }); productVM.SubCategoryList = _unitOfWork.SubCategory.GetAll().Select(i => new SelectListItem { Text = i.Name, Value = i.Id.ToString() }); productVM.SubssList = _unitOfWork.Subss.GetAll().Select(i => new SelectListItem { Text = i.Name, Value = i.Id.ToString() }); if (productVM.Product.Id != 0) { productVM.Product = _unitOfWork.Product.Get(productVM.Product.Id); } } return(View(productVM)); }