public IActionResult Edit(int id) { var cs = unit.CommercialSpaceRepository.GetCommercialSpace(id); EditCommercialSpaceViewModel viewModel = new EditCommercialSpaceViewModel { Address = cs.Address, Advance = cs.Advance, City = cs.City, ConstructionYear = cs.ConstructionYear, Description = cs.FullDescription, Price = cs.Price, SquareMetrage = cs.SquareMetrage, Name = cs.Name, NumberOfRooms = cs.NumberOfRooms, Floor = cs.Floor, Id = cs.CommercialSpaceId, AgencyName = cs.AgencyName, MainImageName = cs.MainImageName }; return(View(viewModel)); }
public IActionResult Edit(EditCommercialSpaceViewModel model) { if (ModelState.IsValid) { var cs = unit.CommercialSpaceRepository.GetCommercialSpace(model.Id); cs.Name = model.Name; cs.Price = model.Price; cs.Address = model.Address; cs.NumberOfRooms = model.NumberOfRooms; cs.SquareMetrage = model.SquareMetrage; cs.City = model.City; cs.ConstructionYear = model.ConstructionYear; cs.Floor = model.Floor; cs.Advance = model.Advance; cs.FullDescription = model.Description; if (model.MainImage != null) { string filePath = Path.Combine(hostingEnvironment.WebRootPath, "images", model.MainImageName); System.IO.File.Delete(filePath); string name = manager.ReturnUniqueName(model.MainImage); manager.UploadPhoto(model.MainImage, Path.Combine(hostingEnvironment.WebRootPath, "images"), name); cs.MainImage = new Photo { PhotoName = name, PhotoPath = Path.Combine(hostingEnvironment.WebRootPath, "images") }; cs.MainImageName = name; } if (model.Images != null) { List <string> photoNames = new List <string>(); foreach (var image in cs.Images) { System.IO.File.Delete(image.PhotoPath); } foreach (var photo in model.Images) { var name = manager.ReturnUniqueName(photo); photoNames.Add(name); manager.UploadPhoto(photo, cs.Images.FirstOrDefault().PhotoPath, name); } foreach (var photoName in photoNames) { cs.Images.Add(new Photo { PhotoName = photoName, PhotoPath = Path.Combine(hostingEnvironment.WebRootPath, "images") }); } } unit.CommercialSpaceRepository.EditCommercialSpace(cs); unit.SaveData(); return(RedirectToAction("Success", "Customers")); } return(View(model)); }