예제 #1
0
        //public async Task<IActionResult> Create(ComicImageViewModel comicImageViewModel);
        public async Task <IActionResult> Create(ComicImageViewModel comicImageViewModel)
        {
            //the comicimageviewmodel does not contain a userid so we need to remove it to make modelstate valid
            ModelState.Remove("comic.UserId");
            if (ModelState.IsValid)
            {
                //get the current user
                var user = await GetCurrentUserAsync();

                //this will add and image to the comic database
                if (comicImageViewModel.ImageFile != null)
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        await comicImageViewModel.ImageFile.CopyToAsync(memoryStream);

                        comicImageViewModel.comic.ComicImage = memoryStream.ToArray();
                    }
                }
                ;

                //add the user id to the comic posted to inventory
                comicImageViewModel.comic.UserId = user.Id;
                _context.Add(comicImageViewModel.comic);

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index), new { id = comicImageViewModel.comic.Id.ToString() }));
            }

            //return the view of comicimageviewmodel
            return(View(comicImageViewModel));
        }
예제 #2
0
        public IActionResult Create()
        {
            //this will create the entry fields set up in the comic model
            ComicImageViewModel comicImageViewModel = new ComicImageViewModel();

            ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id");

            //i used a view model to add images to each comic
            return(View(comicImageViewModel));
        }