Пример #1
0
        public async Task <IActionResult> Edit(int?id)
        {
            /*
             * if (id == null)
             * {
             *  return NotFound();
             * }
             *
             * var book = await _context.Book.FindAsync(id);
             * if (book == null)
             * {
             *  return NotFound();
             * }
             * var viewModel = new BookEditViewModel
             * {
             *  Book = book,
             *  AvailableAuthors = await _context.Author.ToListAsync()
             * };
             * return View(viewModel);
             */
            // if the id that was passed in if null, then an error is thrown
            if (id == null)
            {
                return(NotFound());
            }

            var sneaker = await _context.Sneaker.FindAsync(id);

            if (sneaker == null)
            {// if the sneaker from the db that was passed in if null, then an error is thrown
                return(NotFound());
            }
            var viewModel = new SneakersEditViewModel
            {
                Sneaker             = sneaker,
                AvailableBrands     = await _context.Brand.ToListAsync(),
                AvailableConditions = await _context.Condition.ToListAsync(),
                AvailableSizes      = await _context.Size.ToListAsync()
            };

            return(View(viewModel));
        }
Пример #2
0
        public async Task <IActionResult> Edit(int id, SneakersEditViewModel viewModel)
        {
            var sneaker       = viewModel.Sneaker;
            var sneaker4Image = viewModel.Sneaker.ImgPath;

            if (id != sneaker.SneakerId)
            {
                return(NotFound());
            }

            ModelState.Remove("Sneaker.Brand");
            ModelState.Remove("Sneaker.Condition");
            ModelState.Remove("Sneaker.Size");
            ModelState.Remove("Sneaker.User");
            ModelState.Remove("Sneaker.UserId");

            string uniqueFileName = null;

            if (viewModel.Photo != null)
            {
                string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + viewModel.Photo.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                viewModel.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var currentUser = await GetCurrentUserAsync();

                    sneaker.UserId = currentUser.Id;
                    sneaker.User   = currentUser;
                    if (uniqueFileName != null)
                    {
                        sneaker.ImgPath = uniqueFileName;
                    }
                    else
                    {
                        sneaker.ImgPath = sneaker4Image;
                    }

                    _context.Update(sneaker);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SneakerExists(sneaker.SneakerId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(MySneakers)));
            }
            viewModel.AvailableBrands = await _context.Brand.ToListAsync();

            viewModel.AvailableConditions = await _context.Condition.ToListAsync();

            viewModel.AvailableSizes = await _context.Size.ToListAsync();

            return(View(sneaker));
        }