public ActionResult AddGoods([Bind(Include = "Id,Quantity")] TradingGoods tradingGoods) { var goods = db.TradingGoods.Where(t => t.Id == tradingGoods.Id).FirstOrDefault(); goods.Quantity += tradingGoods.Quantity; // db.Entry(tradingGoods).Property(x => x.Quantity).IsModified = true; db.SaveChanges(); return(RedirectToAction("index")); }
public ActionResult DeleteConfirmed(int id) { TradingGoods TradingGoods = db.TradingGoods.Find(id); var model = db.TradingGoods.Include(t => t.Image).SingleOrDefault(x => x.Id == TradingGoods.Id); foreach (var images in model.Image) { string imagename = images.ImageName.ToString(); var fileName = Path.Combine(Server.MapPath("/uploads/Trading"), Path.GetFileName(imagename)); System.IO.File.Delete(fileName); // db.Database.ExecuteSqlCommand("delete from Images where ImageName='" + imagename + "'"); } try { var fileName1 = Path.Combine(Server.MapPath("~/Images/Thumbail/Trading/"), Path.GetFileName(TradingGoods.Thumbail)); System.IO.File.Delete(fileName1); db.TradingGoods.Remove(TradingGoods); string Operation = "Goods deleted successfully"; db.ActivityLogs.Add(new ActivityLog { Operation = Operation, CreatedBy = Session["userEmail"].ToString(), CreatedDate = DateTime.Now }); db.SaveChanges(); } catch { db.TradingGoods.Remove(TradingGoods); string Operation = "Goods deleted successfully"; db.ActivityLogs.Add(new ActivityLog { Operation = Operation, CreatedBy = Session["userEmail"].ToString(), CreatedDate = DateTime.Now }); db.SaveChanges(); } return(RedirectToAction("Index")); }
// GET: TradingGoods/Delete/5 public ActionResult Delete(int?id) { if (Session["ACategory"] != null) { if (Session["ACategory"].ToString() == "Trading" && Session["ACategory"] != null) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } TradingGoods TradingGoods = db.TradingGoods.Find(id); if (TradingGoods == null) { return(HttpNotFound()); } return(View(TradingGoods)); } } return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); }
// GET: TradingGoods/Edit/5 public ActionResult Edit(int?id) { if (Session["ACategory"] != null) { if (Session["ACategory"].ToString() == "Trading" && Session["ACategory"] != null) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } TradingGoods TradingGoods = db.TradingGoods.Include(s => s.Image).SingleOrDefault(x => x.Id == id); if (TradingGoods == null) { return(HttpNotFound()); } return(View(TradingGoods)); } } return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); }
public ActionResult Edit(TradingGoods TradingGoods, HttpPostedFileBase TImage, ICollection <HttpPostedFileBase> AImage) { if (ModelState.IsValid) { var model = db.TradingGoods.Include(t => t.Image).SingleOrDefault(x => x.Id == TradingGoods.Id); string thumbail = db.TradingGoods.Include(t => t.Image).Where(x => x.Id == TradingGoods.Id).Select(x => x.Thumbail).SingleOrDefault(); try { if (AImage.Any(m => m.ContentLength > 0)) { foreach (var images in model.Image) { string imagename = images.ImageName.ToString(); var fileName = Path.Combine(Server.MapPath("/uploads/Trading/"), Path.GetFileName(imagename)); System.IO.File.Delete(fileName); db.Database.ExecuteSqlCommand("delete from Images where ImageName='" + imagename + "'"); } } } catch { goto Found; } Found: { List <Image> fileDetails = new List <Image>(); foreach (var file in AImage) { if (file != null && file.ContentLength > 0) { var fileName = Path.Combine(Server.MapPath("/uploads/Trading/"), Guid.NewGuid() + Path.GetExtension(file.FileName)); Image image = new Image() { ImageName = "/Uploads/Trading/" + Path.GetFileName(fileName), Path = Path.GetExtension(fileName), TradingGoodsId = TradingGoods.Id, }; fileDetails.Add(image); file.SaveAs(fileName); db.Entry(image).State = EntityState.Added; } } if (TImage != null) { string fileName3 = TImage.FileName; string filename3 = Path.GetFileNameWithoutExtension(TImage.FileName); string extension3 = Path.GetExtension(TImage.FileName); filename3 = TradingGoods.Id + extension3; TradingGoods.Thumbail = "/images/Thumbail/Trading/" + filename3; filename3 = Path.Combine(Server.MapPath("~/Images/Thumbail/Trading/"), filename3); TImage.SaveAs(filename3); } else { TradingGoods.Thumbail = thumbail; } try { db.SaveChanges(); String Operation = "Goods Edited Sucessfully"; db.ActivityLogs.Add(new ActivityLog { Operation = Operation, CreatedBy = Session["userEmail"].ToString(), CreatedDate = DateTime.Now }); // TradingGoods.Image = fileDetails; var objTradingGoods = db.TradingGoods.SingleOrDefault(m => m.Id == TradingGoods.Id); objTradingGoods.GoodId = TradingGoods.GoodId; objTradingGoods.Vat = TradingGoods.Vat; objTradingGoods.CoupenCode = TradingGoods.CoupenCode; objTradingGoods.GoodName = TradingGoods.GoodName; objTradingGoods.LongDetail = TradingGoods.LongDetail; objTradingGoods.Quantity = TradingGoods.Quantity; objTradingGoods.ShortDetail = TradingGoods.ShortDetail; objTradingGoods.Thumbail = TradingGoods.Thumbail; objTradingGoods.WholesaleRate = TradingGoods.WholesaleRate; objTradingGoods.EditedBy = Session["userEmail"].ToString(); objTradingGoods.EditedDate = DateTime.Now; //objTradingGoods.Image = fileDetails;// // db.Entry(TradingGoods).State = EntityState.Modified; db.Entry(objTradingGoods).Property(x => x.Thumbail).IsModified = true; db.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View(TradingGoods)); } } } return(View(TradingGoods)); }
public ActionResult Create(TradingGoods TradingGoods, HttpPostedFileBase TImage, ICollection <HttpPostedFileBase> AImage)//[Bind(Include = "Id,GoodId,Name,ShortDetail,LongDetail,Thumbail,WholesaleRate,Quantity,CoupenCode,CreatedDate,CreatedBy,EditedBy,EditedDate,Imag ) { if (Session["ACategory"] != null) { if (Session["ACategory"].ToString() == "Trading" && Session["ACategory"] != null) { if (ModelState.IsValid) { List <Image> fileDetails = new List <Image>(); foreach (var file in AImage) { if (file != null && file.ContentLength > 0) { string firstpath = "/uploads/"; string subPath = "/uploads/Trading/"; // your code goes here bool imageexist = System.IO.Directory.Exists(Server.MapPath(firstpath)); bool exists = System.IO.Directory.Exists(Server.MapPath(subPath)); if (!imageexist) { System.IO.Directory.CreateDirectory(Server.MapPath(firstpath)); } if (!exists) { System.IO.Directory.CreateDirectory(Server.MapPath(subPath)); } var fileName = Path.Combine(Server.MapPath("/uploads/Trading"), Guid.NewGuid() + Path.GetExtension(file.FileName)); Image image = new Image() { ImageName = "/uploads/Trading" + Path.GetFileName(fileName), Path = Path.GetExtension(fileName), }; fileDetails.Add(image); file.SaveAs(fileName); } } TradingGoods.Image = fileDetails; db.TradingGoods.Add(TradingGoods); db.SaveChanges(); db.Entry(TradingGoods).GetDatabaseValues(); if (TImage != null) { string firstpath = "/Images/"; string subPath = "/Images/Thumbail/"; // your code goes here string TPAth = "/Images/Thumbail/Trading/"; bool imageexist = System.IO.Directory.Exists(Server.MapPath(firstpath)); bool exists = System.IO.Directory.Exists(Server.MapPath(subPath)); bool Texists = System.IO.Directory.Exists(Server.MapPath(TPAth)); if (!imageexist) { System.IO.Directory.CreateDirectory(Server.MapPath(firstpath)); } if (!exists) { System.IO.Directory.CreateDirectory(Server.MapPath(subPath)); } if (!Texists) { System.IO.Directory.CreateDirectory(Server.MapPath(TPAth)); } string fileName3 = TImage.FileName; string filename3 = Path.GetFileNameWithoutExtension(TImage.FileName); string extension3 = Path.GetExtension(TImage.FileName); filename3 = TradingGoods.Id + extension3; TradingGoods.Thumbail = "/Images/Thumbail/Trading/" + filename3; filename3 = Path.Combine(Server.MapPath("~/Images/Thumbail/Trading/"), filename3); TImage.SaveAs(filename3); } TradingGoods.CreatedBy = Session["userEmail"].ToString(); TradingGoods.CreatedDate = DateTime.Now; try { db.TradingGoods.Attach(TradingGoods); db.Entry(TradingGoods).Property(x => x.Thumbail).IsModified = true; db.SaveChanges(); String Operation = "Goods created successfully"; db.ActivityLogs.Add(new ActivityLog { Operation = Operation, CreatedBy = Session["userEmail"].ToString(), CreatedDate = DateTime.Now }); return(RedirectToAction("Index")); } catch { return(View(TradingGoods)); } } var modelStateErrors = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors); return(View(TradingGoods)); } } return(new HttpStatusCodeResult(HttpStatusCode.NotFound)); }