예제 #1
0
        public async Task <IActionResult> Edit(int id, [Bind("EstateId,EstateTypeId,SubEstateType,Region,StatusId,CityId,Price,CreateDuration,Area,BedRooms,BathRooms,Floors,Address,ShortDescription,Description,Tags,Facilities")] Estate estate, IEnumerable <IFormFile> images)
        {
            int userId = _userService.GetUserIdByUserName(User.Identity.Name);

            var pics = _context.EstateImages
                       .Where(e => e.EstateId == estate.EstateId)
                       .ToList();

            estate.UserId     = userId;
            estate.CreateDate = DateTime.Now;
            estate.IsDeleted  = false;
            if (id != estate.EstateId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                if (!images.Any())
                {
                    estate.EstateImages = pics;
                }

                if (images.Any())
                {
                    string imagePath = "";
                    foreach (var names in pics)
                    {
                        imagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\EstateImages",
                                                 names.Image);

                        if (System.IO.File.Exists(imagePath))
                        {
                            System.IO.File.Delete(imagePath);

                            _context.EstateImages.Remove(names);
                        }
                    }


                    foreach (var file in images)
                    {
                        if (file.Length > 0)
                        {
                            var fileName = Path.GetFileName(file.FileName);
                            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\EstateImages", fileName);
                            await using (var fileSteam = new FileStream(filePath, FileMode.Create))
                            {
                                await file.CopyToAsync(fileSteam);
                            }


                            EstateImage estateImage = new EstateImage()
                            {
                                EstateId = id,
                                Image    = fileName
                            };
                            _context.EstateImages.Update(estateImage);
                            await _context.SaveChangesAsync();
                        }
                    }
                }

                try
                {
                    _context.Update(estate);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EstateExists(estate.EstateId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CityId"]       = new SelectList(_context.Cities, "CityId", "CityName", estate.CityId);
            ViewData["EstateTypeId"] = new SelectList(_context.EstateTypes, "EstateTypeId", "Type", estate.EstateTypeId);
            ViewData["UserId"]       = new SelectList(_context.Users, "UserId", "Email", estate.UserId);
            return(View(estate));
        }
예제 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("BlogId,Title,ShortDescription,Description,ImageName")] Blog blog, IFormFile image)
        {
            var userId = _userService.GetUserIdByUserName(User.Identity.Name);

            blog.UserId     = userId;
            blog.CreateDate = DateTime.Now;
            blog.IsDeleted  = false;
            if (id != blog.BlogId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                if (image == null)
                {
                    blog.ImageName = blog.ImageName;
                }
                else
                {
                    string imagePath = "";
                    if (blog.ImageName != image.FileName)
                    {
                        imagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\BlogImages", blog.ImageName);
                        if (System.IO.File.Exists(imagePath))
                        {
                            System.IO.File.Delete(imagePath);
                        }
                    }

                    if (image != null && image.Length > 0)
                    {
                        var fileName = Path.GetFileName(image.FileName);
                        var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\BlogImages", fileName);
                        await using (var fileSteam = new FileStream(filePath, FileMode.Create))
                        {
                            await image.CopyToAsync(fileSteam);
                        }
                        blog.ImageName = fileName;
                    }
                }

                try
                {
                    _context.Update(blog);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BlogExists(blog.BlogId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(blog));
        }