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 })); }