예제 #1
0
        // GET: Comics/Edit/5
        //edit comic by comic id
        public async Task <IActionResult> Edit(int?id)
        {
            //if no comic id return notfound
            if (id == null)
            {
                return(NotFound());
            }
            //get the comic id
            var comic = await _context.Comics
                        .FindAsync(id);

            if (comic == null)
            {
                return(NotFound());
            }
            //use the editcomicimageviewmodel to be able to add a new image to comic
            var viewModel = new EditComicImageViewModel()
            {
                comic = comic
            };

            //return the view of the viewmodel
            return(View(viewModel));
        }
예제 #2
0
        public async Task <IActionResult> Edit(int id, EditComicImageViewModel viewModel)
        {
            //use the editcomicimageviewmodel to edit a comic with an image get the comic id
            var comicWithImage = await _context.Comics.AsNoTracking().FirstOrDefaultAsync(c => c.Id == id);

            //if comic id cant be found return not found
            if (id != viewModel.comic.Id)
            {
                return(NotFound());
            }
            //remove the userid because the editcomicimageviewmodel does not have userid
            ModelState.Remove("comic.UserId");
            if (ModelState.IsValid)
            {
                try
                {
                    //get the current user
                    var currentUser = await GetCurrentUserAsync();

                    //check to see if the comic in inventory has an image attached view the image in the edit page
                    if (viewModel.ImageFile == null && comicWithImage.ComicImage != null)
                    {
                        viewModel.comic.ComicImage = comicWithImage.ComicImage;
                    }

                    //if no image add one to the comic inventory
                    else
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            await viewModel.ImageFile.CopyToAsync(memoryStream);

                            viewModel.comic.ComicImage = memoryStream.ToArray();
                        }
                    }
                    ;
                    //add the user id, update the new image and data, and save the changes
                    viewModel.comic.UserId = currentUser.Id;
                    _context.Update(viewModel.comic);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    //if no comic exists with a matching id in the inventory return not found
                    if (!ComicExists(viewModel.comic.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                //if comic edit page is updated properly and submited return user back to comic inventory index
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
            }
            //if comic is not updated properly return view of the edit comic by id page
            return(View(viewModel));
        }