コード例 #1
0
        // create logic should be done
        #region create
        // GET: Pokemons/Create
        public ActionResult Create()
        {
            var pokemon         = new MyPokemon();
            var pokemonCreateVm = new PokemonCreateVm();

            pokemon.PokemonTypes = new List <PokemonType>();
            PopulateAssignedTypeData(pokemon);

            return(View(pokemonCreateVm));
        }
コード例 #2
0
        public ActionResult Create(PokemonCreateVm pokemonCreateVm, MyPokemon createdPokemon, string[] selectedTypes)
        {
            createdPokemon = new MyPokemon
            {
                Name           = pokemonCreateVm.Name,
                HasAllolanForm = pokemonCreateVm.HasAllolanForm,
                ImgUrl         = pokemonCreateVm.ImgUrl,
                Description    = pokemonCreateVm.Description,
                Location       = pokemonCreateVm.location,
                NDex           = pokemonCreateVm.NDex,
                Id             = Guid.NewGuid()
            };

            if (selectedTypes != null)
            {
                createdPokemon.PokemonTypes = new List <PokemonType>();
                foreach (var type in selectedTypes)
                {
                    var typeToAdd = _pokemonContext.Types.Find(Guid.Parse(type));
                    var foo       = new PokemonType {
                        TypeId = typeToAdd.Id, PokemonId = createdPokemon.Id, Type = typeToAdd
                    };
                    createdPokemon.PokemonTypes.Add(foo);
                }
            }

            if (ModelState.IsValid)
            {
                createdPokemon.ImgUrl = SavePokeImg(pokemonCreateVm.UploadedImage);
                _pokemonContext.Pokemons.Add(createdPokemon);
                _pokemonContext.SaveChanges();
                return(RedirectToAction("Index"));
            }
            PopulateAssignedTypeData(createdPokemon);
            return(View(pokemonCreateVm));
        }