Exemple #1
0
        public async Task <IActionResult> Create(int id)
        {
            //Creates a view model so we can store multiple resources and pass them to our view
            CaughtPokemonCreateViewModel model = new CaughtPokemonCreateViewModel();

            //Fetches the Pokemon that the user has selected to catch
            model.SelectedPokemon = await _context.Pokemon
                                    .Where(p => p.Id == id)
                                    .FirstOrDefaultAsync();

            if (model.SelectedPokemon == null)
            {
                return(NotFound());
            }

            //A blank CaughtPokemon resource was created with our ViewModel. This resource needs a PokemonId so that it can be properly created when the user clicks Submit
            model.PokemonToAdd.PokemonId = id;

            //Fetches our three gender selections from the database - Male, Female, and Genderless
            List <Gender> gendersToSelectFrom = await _context.Gender.ToListAsync();

            //Turns those fetched Genders into SelectListItems, so we can use them in a dropdown
            model.Genders = new SelectList(gendersToSelectFrom, "Id", "Name").ToList();

            //Passes our completed ViewModel to the View
            return(View(model));
        }
Exemple #2
0
        public async Task <IActionResult> Create(CaughtPokemonCreateViewModel model)
        {
            //The use of a ViewModel means that there are certain data values that are left null when this method is called. We still want to check if our ModelState is (mostly) valid, but these null fields will throw off our logic. So, let's remove them from the ModelState. I promise, they're not useful. A lot come from SelectedPokemon (the pre-seeded Pokemon resource in our database), while two are related to User data (which hasn't been added yet)
            ModelState.Remove("User");
            ModelState.Remove("UserId");
            ModelState.Remove("SelectedPokemon.Name");
            ModelState.Remove("SelectedPokemon.Type1");
            ModelState.Remove("SelectedPokemon.RBSpriteURL");
            ModelState.Remove("SelectedPokemon.OfficialArtURL");
            ModelState.Remove("SelectedPokemon.DefaultSpriteURL");

            //Runs if our ModelState is valid and has no inappropriate null values
            if (ModelState.IsValid)
            {
                //Gets the current user and sets the new resource's user ID to match
                ApplicationUser currentUser = await GetCurrentUserAsync();

                model.PokemonToAdd.UserId = currentUser.Id;


                //Adds the CaughtPokemon resource (which is on our ViewModel) to the database. Then sends us to the Dex view.
                _context.Add(model.PokemonToAdd);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Dex", "Pokemon"));
            }

            //If our CaughtPokemon is not successfully added to the database, we're going to fetch a ViewModel just like in our GET Create method, but call it failedModel instead

            CaughtPokemonCreateViewModel failedModel = new CaughtPokemonCreateViewModel();

            failedModel.SelectedPokemon = await _context.Pokemon
                                          .Where(p => p.Id == model.PokemonToAdd.PokemonId)
                                          .FirstOrDefaultAsync();

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

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

            //Refreshes the Create view with this failedModel
            return(View(failedModel));
        }