Esempio n. 1
0
        public async Task <IActionResult> Edit(int?id)
        {
            //First off, if this method is called with no ID, return a NotFound page
            if (id == null)
            {
                return(NotFound());
            }

            //Gets the current user and creates a ViewModel
            ApplicationUser currentUser = await GetCurrentUserAsync();

            CaughtPokemonEditViewModel model = new CaughtPokemonEditViewModel();

            //Add to our ViewModel: we're fetching the CaughtPokemon that the user has selected to edit. We're including their Gender and Pokemon info, and checking the UserId to make sure users can't edit another user's Pokemon
            model.SelectedCaughtPokemon = await _context.CaughtPokemon
                                          .Include(cp => cp.Pokemon)
                                          .Include(cp => cp.Gender)
                                          .Where(cp => cp.UserId == currentUser.Id)
                                          .Where(cp => cp.Id == id)
                                          .FirstOrDefaultAsync();

            //If the user is trying to access a Pokemon that doesn't exist, return a Not Found page
            if (model.SelectedCaughtPokemon == null)
            {
                return(NotFound());
            }

            //We're now grabbing a list of every Pokemon possible, so we can let the user edit their Pokemon's species with a dropdown. In the games, Pokemon can evolve to a new species but retain their nickname, level, etc. so this edit feature is a handy way to handle such a scenario
            List <Pokemon> allPokemonToSelectFrom = await _context.Pokemon
                                                    .ToListAsync();

            //Creates a list of SelectListItems with each Pokemon's name and ID
            List <SelectListItem> lowercasePokemonChoices = new SelectList(allPokemonToSelectFrom, "Id", "Name", model.SelectedCaughtPokemon.PokemonId).ToList();

            //When I scraped PokeAPI to create my database, the Pokemon's names were all lowercase! So for this dropdown, we'd like their names to be uppercase. Let's begin by looping through the list we just made
            foreach (SelectListItem lowercaseChoice in lowercasePokemonChoices)
            {
                //Create a NEW SelectListItem with the same value and selected statuses as the original. BUT! The text will be a concatenated version of the lowercase string, with the first character capitalized
                SelectListItem uppercaseChoice = new SelectListItem()
                {
                    Selected = lowercaseChoice.Selected,
                    Text     = char.ToUpper(lowercaseChoice.Text[0]) + lowercaseChoice.Text.Substring(1),
                    Value    = lowercaseChoice.Value
                };

                //Adds the uppercase SelectListItem to our list in our ViewModel
                model.AllPokemon.Add(uppercaseChoice);
            }

            //Just like in the Create method, we're going to fetch our genders and make SelectListItems out of them
            List <Gender> gendersToSelectFrom = await _context.Gender.ToListAsync();

            model.Genders = new SelectList(gendersToSelectFrom, "Id", "Name", model.SelectedCaughtPokemon.GenderId).ToList();

            //Sends our ViewModel to our View
            return(View(model));
        }
Esempio n. 2
0
        public async Task <IActionResult> Edit(int id, CaughtPokemonEditViewModel model)
        {
            //If the user has somehow mismatched the Pokemon IDs, returns a NotFound page
            if (id != model.SelectedCaughtPokemon.Id)
            {
                return(NotFound());
            }

            //Checks if the ModelState is valid
            if (ModelState.IsValid)
            {
                try
                {
                    //Adds the updated CaughtPokemon from out ViewModel and sends it to our database
                    _context.Update(model.SelectedCaughtPokemon);
                    await _context.SaveChangesAsync();
                }
                //Exception handling if the update fails
                catch (DbUpdateConcurrencyException)
                {
                    if (!CaughtPokemonExists(model.SelectedCaughtPokemon.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                //Once we edit a Pokemon in our Collection, return the user to their Collection view
                return(RedirectToAction(nameof(Collection)));
            }

            //If the edit fails, we're going to re-update our ViewModel and refresh the page. Much like the POST Create method, but out ViewModel is filled like in our GET Edit method instead.

            ApplicationUser currentUser = await GetCurrentUserAsync();

            CaughtPokemonEditViewModel failedModel = new CaughtPokemonEditViewModel();

            failedModel.SelectedCaughtPokemon = await _context.CaughtPokemon
                                                .Include(cp => cp.Pokemon)
                                                .Include(cp => cp.Gender)
                                                .Where(cp => cp.UserId == currentUser.Id)
                                                .Where(cp => cp.Id == id)
                                                .FirstOrDefaultAsync();

            if (failedModel.SelectedCaughtPokemon == null)
            {
                return(NotFound());
            }

            List <Pokemon> allPokemonToSelectFrom = await _context.Pokemon
                                                    .ToListAsync();

            List <SelectListItem> lowercasePokemonChoices = new SelectList(allPokemonToSelectFrom, "Id", "Name", failedModel.SelectedCaughtPokemon.PokemonId).ToList();

            foreach (SelectListItem lowercaseChoice in lowercasePokemonChoices)
            {
                SelectListItem uppercaseChoice = new SelectListItem()
                {
                    Selected = lowercaseChoice.Selected,
                    Text     = char.ToUpper(lowercaseChoice.Text[0]) + lowercaseChoice.Text.Substring(1),
                    Value    = lowercaseChoice.Value
                };

                failedModel.AllPokemon.Add(uppercaseChoice);
            }

            List <Gender> gendersToSelectFrom = await _context.Gender.ToListAsync();

            failedModel.Genders = new SelectList(gendersToSelectFrom, "Id", "Name", failedModel.SelectedCaughtPokemon.GenderId).ToList();

            return(View(failedModel));
        }