public ActionResult Edit([Bind(Include = "ID,Product_Name,Producer_ID,Price,Quantity,Category_ID")] Product product, HttpPostedFileBase ImageUrl) { var action = new CheckController().CheckStatus("Products"); if (action != null) { return(action); } try { if (ModelState.IsValid) { Product org = db.Get(product.ID); if (org != null) { product.Picture = org.Picture; } if (ImageUrl != null) { if (System.IO.File.Exists($"~/Content/Images/Products/{org.Picture}")) { System.IO.File.Delete($"~/Content/Images/Products/{org.Picture}"); } var str = ""; string ImageName = Path.GetFileName(ImageUrl.FileName); do { str = GetName(); } while (System.IO.File.Exists($"~/Content/Images/Products/{str}{ImageName}")); string physicalPath = Server.MapPath($"~/Content/Images/Products/{str}{ImageName}"); ImageUrl.SaveAs(physicalPath); product.Picture = $"{str}{ImageName}"; } db.Update(product); return(RedirectToAction("Index")); } } catch (Exception ex) { ModelState.AddModelError("", ex.Message); } ViewBag.Category_ID = new SelectList(MC.Get_All().Where(i => i.Name.CompareTo("Unknown") != 0).ToList(), "ID", "Name", product.Category_ID); ViewBag.Producer_ID = new SelectList(MP.Get_All().Where(i => i.Name.CompareTo("Unknown") != 0).ToList(), "ID", "Name", product.Producer_ID); return(View(product)); }