Beispiel #1
0
        public async Task <IActionResult> Edit(EditHomeViewModel model)
        {
            var home = await context.Homes.FindAsync(model.Id);

            home.Price       = model.Price;
            home.Rooms       = model.Rooms;
            home.Address     = model.Address;
            home.BuildDate   = model.BuildDate;
            home.CategoryId  = model.CategoryId;
            home.CityId      = model.CityId;
            home.Description = model.Description;

            if (model.Images != null)
            {
                foreach (var file in model.Images)
                {
                    string fileName = await AddFile(file);

                    await context.HomeImages.AddAsync(new HomeImage { HomeId = home.Id, ImagePath = fileName });
                }
            }

            await context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
        public IActionResult Edit(int id)
        {
            var home = unit.HomeRepository.GetHome(id);
            EditHomeViewModel viewModel = new EditHomeViewModel
            {
                Address          = home.Address,
                Advance          = home.Advance,
                City             = home.City,
                ConstructionYear = home.ConstructionYear,
                Description      = home.FullDescription,
                Price            = home.Price,
                SquareMetrage    = home.SquareMetrage,
                Name             = home.Name,
                NumberOfRooms    = home.NumberOfRooms,
                MainImageName    = home.MainImageName,
                Id              = home.HomeId,
                NumberOfFloors  = home.NumberOfFloors,
                TotalArea       = home.TotalArea,
                HaveFurnishings = home.HaveFurnishings,
                HaveGarage      = home.HaveGarage,
                AgencyName      = home.AgencyName
            };

            return(View(viewModel));
        }
Beispiel #3
0
        public async Task <IActionResult> Edit(int id)
        {
            var home = await context.Homes.FindAsync(id);

            var homeImages = await context.HomeImages.Where(x => x.HomeId == home.Id).ToDictionaryAsync(x => x.Id, x => x.ImagePath);

            var editHomeViewModel = new EditHomeViewModel
            {
                Id          = id,
                Address     = home.Address,
                BuildDate   = home.BuildDate,
                CategoryId  = home.CategoryId,
                CityId      = home.CityId,
                Description = home.Description,
                PrevImages  = homeImages,
                Price       = home.Price,
                Rooms       = home.Rooms,
                Categories  = await context.Categories.ToDictionaryAsync(x => x.Id, x => x.Name),
                Cities      = await context.Cities.ToDictionaryAsync(x => x.Id, x => x.Name)
            };

            return(View(editHomeViewModel));
        }
        public IActionResult Edit(EditHomeViewModel model)
        {
            if (ModelState.IsValid)
            {
                var home = unit.HomeRepository.GetHome(model.Id);

                home.Name             = model.Name;
                home.Price            = model.Price;
                home.Address          = model.Address;
                home.NumberOfRooms    = model.NumberOfRooms;
                home.SquareMetrage    = model.SquareMetrage;
                home.City             = model.City;
                home.ConstructionYear = model.ConstructionYear;
                home.NumberOfFloors   = model.NumberOfFloors;
                home.Advance          = model.Advance;
                home.FullDescription  = model.Description;
                home.TotalArea        = model.TotalArea;
                home.HaveFurnishings  = model.HaveFurnishings;
                home.HaveGarage       = model.HaveGarage;

                if (model.MainImage != null)
                {
                    string filePath = Path.Combine(environment.WebRootPath,
                                                   "images", model.MainImageName);
                    System.IO.File.Delete(filePath);
                    string name = manager.ReturnUniqueName(model.MainImage);
                    manager.UploadPhoto(model.MainImage, Path.Combine(environment.WebRootPath,
                                                                      "images"), name);
                    home.MainImage = new Photo
                    {
                        PhotoName = name,
                        PhotoPath = Path.Combine(environment.WebRootPath, "images")
                    };
                    home.MainImageName = name;
                }
                if (model.Images != null)
                {
                    List <string> photoNames = new List <string>();

                    foreach (var image in home.Images)
                    {
                        System.IO.File.Delete(image.PhotoPath);
                    }
                    foreach (var photo in model.Images)
                    {
                        var name = manager.ReturnUniqueName(photo);
                        photoNames.Add(name);
                        manager.UploadPhoto(photo, home.Images.FirstOrDefault().PhotoPath, name);
                    }
                    foreach (var photoName in photoNames)
                    {
                        home.Images.Add(new Photo {
                            PhotoName = photoName, PhotoPath = Path.Combine(environment.WebRootPath, "images")
                        });
                    }
                }

                unit.HomeRepository.EditHome(home);
                unit.SaveData();
                return(RedirectToAction("Success", "Customers"));
            }

            return(View(model));
        }