public async Task <IActionResult> Edit(int reportId) { var report = await _context.TripReports .Include(i => i.TripReportImages) .Include(s => s.TripBooking.SchedulingTripDetail) .FirstOrDefaultAsync(r => r.Id == reportId); var model = new TripReportEditModelView() { Id = report.Id, TripBookingId = report.TripBookingId, TextBody = report.TextBody, Images = report.TripReportImages.ToList(), DitailId = report.TripBooking.SchedulingTripDetailId }; return(View(model)); }
public async Task <IActionResult> Edit(int reportId, TripReportEditModelView tripReport) { if (ModelState.IsValid) { if (tripReport.Photos != null) { if (tripReport.Photos.Count() >= 5 || tripReport.Photos.Count() > 10) { foreach (var ph in tripReport.Photos) { if (!IsFileValidate(ph.FileName)) { ViewBag.msg = "الصور المسموح بها يجب ان تكون بمتداد : " + "png , jpeg , jpg , gif , bmp "; return(View(tripReport)); } } } else { ViewBag.msg = "الحد الأقل المسموح به 5 صور والأعلى 10 صور"; return(View(tripReport)); } } var repo = new TripReport() { Id = tripReport.Id, TripBookingId = tripReport.TripBookingId, TextBody = tripReport.TextBody }; _context.Update(repo); await _context.SaveChangesAsync(); if (tripReport.Photos != null) { string uniqFileName = null; var modelImagelist = new List <TripReportImage>(); string sImage_Folder = "reports"; string sPath_WebRoot = _ihostingEnvironment.WebRootPath; string sPath_of_Target_Folder = sPath_WebRoot + "\\img\\" + sImage_Folder + "\\"; string sFile_Target_Original = sPath_of_Target_Folder + "Original\\"; if (tripReport.Photos.Count > 0 && tripReport.Photos != null) { var imagesFoDelete = await _context.TripReportImages.Where(i => i.TripReportId == repo.Id).ToListAsync(); foreach (var img in imagesFoDelete) { string OriginalDelete = Path.Combine(_ihostingEnvironment.WebRootPath, "img/reports/Original", img.ImagePath); System.IO.File.Delete(OriginalDelete); string Scale400lDelete = Path.Combine(_ihostingEnvironment.WebRootPath, "img/reports/400", img.ImagePath); System.IO.File.Delete(Scale400lDelete); _context.Remove(img); } await _context.SaveChangesAsync(); foreach (IFormFile photo in tripReport.Photos) { string uplouadsFolder = Path.Combine(_ihostingEnvironment.WebRootPath, "img/reports/Original"); uniqFileName = Guid.NewGuid().ToString() + "_" + photo.FileName; string filePath = Path.Combine(uplouadsFolder, uniqFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { photo.CopyTo(fileStream); } Image_resize(sFile_Target_Original + uniqFileName, sPath_of_Target_Folder + "\\400\\" + uniqFileName, 400); var modelImage = new TripReportImage() { TripReportId = repo.Id, ImagePath = uniqFileName }; modelImagelist.Add(modelImage); } _context.AddRangeAsync(modelImagelist); await _context.SaveChangesAsync(); } } return(RedirectToAction("DetailsMore", "SchedulingTripDetails", new { id = repo.TripBookingId })); } return(View(tripReport)); }