public IActionResult PostImage(ImageInput image) { var imageProduct = new ImageMv(); imageProduct.CreateBy = Guid.Parse("a845b16a-4ca6-48e2-4ca6-08d817450c1a"); imageProduct.ProductId = image.ProductId; //convert to image to base 64 var ms = new MemoryStream(); image.FileInput.CopyTo(ms); var fileBytes = ms.ToArray(); string s = Convert.ToBase64String(fileBytes); imageProduct.FileInput = s; if (ImageBus.PostImage(imageProduct).Result) { TempData[ConstKey.Success] = "Add Success!"; } else { TempData[ConstKey.Error] = "Fail, Try again !"; } return(RedirectToAction("ForProduct", "ImageProduct", new { productId = image.ProductId })); }
public JsonResult ChangeStatus(int id) { var result = ImageBus.ChangeStatus(id); return(Json(new { status = result })); }
public JsonResult LoadDetails(int id) { var image = ImageBus.Find(id); return(Json(new { data = image, status = true }, JsonRequestBehavior.AllowGet)); }
public IActionResult DeleteImage(Guid id, Guid productId) { if (ImageBus.DeleteImage(id).Result) { TempData[ConstKey.Success] = "Deleted"; } else { TempData[ConstKey.Error] = "Fail, TryAgain!"; } return(RedirectToAction("ForProduct", "ImageProduct", new { productId })); }
public JsonResult LoadData(int id, string status) { var data = ImageBus.List(id); if (!string.IsNullOrEmpty(status)) { var statusBool = bool.Parse(status); data = data.Where(x => x.Status == statusBool); } return(Json(new { data = data, status = true }, JsonRequestBehavior.AllowGet)); }
// GET: Product/Details/5 public ActionResult Details(int?id) { if (id == null) { return(Redirect("/")); } var product = ProductBus.Find(id); if (product == null) { return(Redirect("/")); } ViewBag.Images = ImageBus.List(id); return(View(ProductBus.Details(id))); }
public JsonResult SaveData(string name, string description, int id, int brand, int category, int type, int price, int quantity, int warranty) { bool status = false; string message = string.Empty; var model = new Product(); model.Name = name; model.Id = id; model.Description = description; model.Price = price; model.Quantity = quantity; model.Warranty = warranty; model.BrandId = brand; model.CateId = category; model.TypeId = type; if (HttpContext.Request.Files.Count > 0) { // TODO: Add insert logic here var hpf = HttpContext.Request.Files[0]; if (hpf.ContentLength > 0) { string fileName = Guid.NewGuid().ToString(); string fullPathWithFileName = "/Data/Images/" + fileName + ".jpg"; hpf.SaveAs(Server.MapPath(fullPathWithFileName)); model.ImageUrl = fullPathWithFileName; } } //add new productgory if id = 0 if (model.Id == 0) { model.DateCreated = DateTime.Now; model.DateModified = DateTime.Now; model.Status = true; model.Viewed = 0; model.Sold = 0; try { ProductBus.Add(model); for (int i = 1; i < HttpContext.Request.Files.Count; i++) { var proImage = new Image(); var pro = ProductBus.Find(model.Id); proImage.ProductId = pro.Id; proImage.Status = true; var hrf = HttpContext.Request.Files[i]; if (hrf.ContentLength > 0) { string fileName = Guid.NewGuid().ToString(); string fullPathWithFileName = "/Data/Images/" + fileName + ".jpg"; hrf.SaveAs(Server.MapPath(fullPathWithFileName)); proImage.ImageUrl = fullPathWithFileName; ImageBus.Add(proImage); } } status = true; } catch (Exception ex) { status = false; message = ex.Message; } } else { //update existing DB //save db var entity = ProductBus.Find(model.Id); if (HttpContext.Request.Files.Count > 0) { // TODO: Add insert logic here var hpf = HttpContext.Request.Files[0]; if (hpf.ContentLength > 0) { string fileName = Guid.NewGuid().ToString(); string fullPathWithFileName = "/Data/Images/" + fileName + ".jpg"; hpf.SaveAs(Server.MapPath(fullPathWithFileName)); entity.ImageUrl = fullPathWithFileName; } } entity.DateModified = DateTime.Now; entity.Name = model.Name; entity.BrandId = model.BrandId; entity.CateId = model.CateId; entity.TypeId = model.TypeId; entity.Price = model.Price; entity.Quantity = model.Quantity; entity.Description = model.Description; entity.Warranty = model.Warranty; entity.DateModified = DateTime.Now; try { ProductBus.Edit(entity); status = true; } catch (Exception ex) { status = false; message = ex.Message; } } return(Json(new { status = status, message = message })); }
public JsonResult SaveData(int imgId, int proId) { bool status = false; string message = string.Empty; if (imgId == 0) { if (HttpContext.Request.Files.Count > 0) { try { for (int i = 1; i < HttpContext.Request.Files.Count; i++) { var img = new Image(); img.ProductId = proId; img.Status = true; var hrf = HttpContext.Request.Files[i]; if (hrf.ContentLength > 0) { string fileName = Guid.NewGuid().ToString(); string fullPathWithFileName = "/Data/Images/" + fileName + ".jpg"; hrf.SaveAs(Server.MapPath(fullPathWithFileName)); img.ImageUrl = fullPathWithFileName; ImageBus.Add(img); } } status = true; } catch (Exception ex) { status = false; message = ex.Message; } } } else { if (HttpContext.Request.Files.Count > 0) { try { var img = ImageBus.Find(proId); var hpf = HttpContext.Request.Files[0]; if (hpf.ContentLength > 0) { string fileName = Guid.NewGuid().ToString(); string fullPathWithFileName = "/Data/Images/" + fileName + ".jpg"; hpf.SaveAs(Server.MapPath(fullPathWithFileName)); img.ImageUrl = fullPathWithFileName; } ImageBus.Edit(img); status = true; } catch (Exception ex) { status = false; message = ex.Message; } } } return(Json(new { status = status, message = message })); }
public IActionResult ForProduct(Guid productId) { ViewBag.Product = ImageBus.GetByProductId(productId).Result; return(View()); }