public async Task <ActionResult> Create([Bind("HotelRoomId,FullName,Images,Amount,Acreage")] HotelRoomsCreateViewModel model) { string nameImages = ""; if (ModelState.IsValid) { string uploadFolder = Path.Combine(webHostEnvironment.WebRootPath, "images"); var fileName = $"{Guid.NewGuid()}_{model.Images.FileName}"; nameImages = fileName; var filePath = Path.Combine(uploadFolder, fileName); using (var fs = new FileStream(filePath, FileMode.Create)) { model.Images.CopyTo(fs); } } var hotel = new HotelRoom() { FullName = model.FullName, Amount = model.Amount, Acreage = model.Acreage }; hotel.Images = nameImages; _context.HotelRooms.Add(hotel); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Edit(int id, [Bind("Id,FullName,Images,Amount,Acreage,urlImages")] HotelRoomsCreateViewModel model) { string nameImages = ""; if (ModelState.IsValid) { if (model.Images != null) { string uploadFolder = Path.Combine(webHostEnvironment.WebRootPath, "images"); var fileName = $"{Guid.NewGuid()}_{model.Images.FileName}"; nameImages = fileName; var filePath = Path.Combine(uploadFolder, fileName); using (var fs = new FileStream(filePath, FileMode.Create)) { model.Images.CopyTo(fs); } } } var hotel = new HotelRoom() { HotelRoomId = model.Id, FullName = model.FullName, Amount = model.Amount, Acreage = model.Acreage }; if (model.Images != null) { hotel.Images = nameImages; } else { for (int i = 8; i < model.urlImages.Length; i++) { hotel.Images += model.urlImages[i]; } } _context.HotelRooms.Update(hotel); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); }
// GET: HotelRooms/Details/5 public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } var hotelRoom = await _context.HotelRooms.FindAsync(id); HotelRoomsCreateViewModel hotel = new HotelRoomsCreateViewModel(); hotel.Acreage = hotelRoom.Acreage; hotel.Amount = hotelRoom.Amount; hotel.FullName = hotelRoom.FullName; hotel.urlImages = "/images/" + hotelRoom.Images; hotel.Id = hotelRoom.HotelRoomId; if (hotelRoom == null) { return(NotFound()); } return(View(hotelRoom)); }