public ActionResult Edit(ItemDTO obj, IFormFile files1, IFormFile files2, IFormFile files3, IFormFile files4, int id) { var itemcategoryid = HttpContext.Request.Form["ItemCategoryId"].ToString(); obj.ItemCategoryId = Convert.ToInt32(itemcategoryid); if (ModelState.IsValid) { try { if (files1 != null) { var selectImageUrl = _db.Items.Where(x => x.ItemId == obj.ItemId).Select(x => x.ImageUrl1).SingleOrDefault(); var completepath = _hostingEnvironment.WebRootPath + "\\itemImages\\" + obj.ItemCategoryId + selectImageUrl; if (System.IO.File.Exists(completepath)) { System.IO.File.Delete(completepath); } var filepath = Path.GetTempFileName(); string filename = ContentDispositionHeaderValue.Parse(files1.ContentDisposition).FileName.Trim('"'); filename = this.EnsureFilename(filename); using (FileStream filestream = System.IO.File.Create(this.Getpath(filename, id, obj))) { files1.CopyToAsync(filestream); obj.ImageUrl1 = "\\" + obj.SignUpId + "\\" + obj.ItemName + "\\" + filename; } } //Second Image //////////////////////////////////////////////// if (files2 != null) { var selectImageUrl = _db.Items.Where(x => x.ItemId == obj.ItemId).Select(x => x.ImageUrl2).SingleOrDefault(); var completepath = _hostingEnvironment.WebRootPath + "\\itemImages\\" + obj.ItemCategoryId + selectImageUrl; if (System.IO.File.Exists(completepath)) { System.IO.File.Delete(completepath); } var filepath = Path.GetTempFileName(); string filename = ContentDispositionHeaderValue.Parse(files2.ContentDisposition).FileName.Trim('"'); filename = this.EnsureFilename(filename); using (FileStream filestream = System.IO.File.Create(this.Getpath(filename, id, obj))) { files2.CopyToAsync(filestream); obj.ImageUrl2 = "\\" + obj.SignUpId + "\\" + obj.ItemName + "\\" + filename; } } //Third image ///////////////////////////////////////////////// if (files3 != null) { var selectImageUrl = _db.Items.Where(x => x.ItemId == obj.ItemId).Select(x => x.ImageUrl3).SingleOrDefault(); var completepath = _hostingEnvironment.WebRootPath + "\\itemImages\\" + obj.ItemCategoryId + selectImageUrl; if (System.IO.File.Exists(completepath)) { System.IO.File.Delete(completepath); } var filepath = Path.GetTempFileName(); string filename = ContentDispositionHeaderValue.Parse(files3.ContentDisposition).FileName.Trim('"'); filename = this.EnsureFilename(filename); using (FileStream filestream = System.IO.File.Create(this.Getpath(filename, id, obj))) { files3.CopyToAsync(filestream); obj.ImageUrl3 = "\\" + obj.SignUpId + "\\" + obj.ItemName + "\\" + filename; } } //Fourth Image /////////////////////////////////////////////// if (files4 != null) { var selectImageUrl = _db.Items.Where(x => x.ItemId == obj.ItemId).Select(x => x.ImageUrl4).SingleOrDefault(); var completepath = _hostingEnvironment.WebRootPath + "\\itemImages\\" + obj.ItemCategoryId + selectImageUrl; if (System.IO.File.Exists(completepath)) { System.IO.File.Delete(completepath); } var filepath = Path.GetTempFileName(); string filename = ContentDispositionHeaderValue.Parse(files4.ContentDisposition).FileName.Trim('"'); filename = this.EnsureFilename(filename); using (FileStream filestream = System.IO.File.Create(this.Getpath(filename, id, obj))) { files4.CopyToAsync(filestream); obj.ImageUrl4 = "\\" + obj.SignUpId + "\\" + obj.ItemName + "\\" + filename; } } // TODO: Add update logic here _itemData.EditedItem(obj); return(RedirectToAction("Index", "Shop", new { id = id })); } catch (Exception ex) { throw; } } return(View()); }