Пример #1
0
        public async Task <IActionResult> Create([Bind("Id,Name,Description,ClassType")] Region region)
        {
            if (ModelState.IsValid)
            {
                _context.Add(region);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(region));
        }
Пример #2
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Types @type)
        {
            if (ModelState.IsValid)
            {
                _context.Add(@type);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(@type));
        }
Пример #3
0
        public async Task<IActionResult> Create([Bind("Id,Name,Description,Gender,Height,Weight,RegionId,PhotoPath,AttackIds,TypesIds")] PokemonCreateViewModel pokemon)
        {
            if (ModelState.IsValid)
            {
                if (pokemon.RegionId==0)
                {
                    ViewBag.ErrorTitle = $"Región no seleccionada para crear Pokemon {pokemon.Name}";
                    ViewBag.ErrorMessage = $"Para crear pokemon {pokemon.Name} debe seleccionar una región a la cual pertenece" +
                        $". Seleccione una región y luego trate de nuevo" +
                        $".";
                    return View("Error1");
                }
                if (pokemon.Gender == null)
                {
                    ViewBag.ErrorTitle = $"Genero no seleccionado para crear Pokemon {pokemon.Name}";
                    ViewBag.ErrorMessage = $"Para crear pokemon {pokemon.Name} debe seleccionar un Genero al cual pertenece" +
                        $". Seleccione una genero y luego trate de nuevo" +
                        $".";
                    return View("Error1");
                }
                if (pokemon.AttackIds.Count>4 || pokemon.AttackIds.Count < 1)
                {
                    ViewBag.ErrorTitle = $"Cantidad requerida de ataques seleccionados para crear Pokemon {pokemon.Name}";
                    ViewBag.ErrorMessage = $"Para crear pokemon {pokemon.Name} solo debe seleccionar entre 1 y 4 ataques"+
                        $". Seleccione y luego trate de nuevo" +
                        $".";
                    return View("Error1");
                }
                if (pokemon.TypesIds.Count > 2 || pokemon.AttackIds.Count < 1)
                {
                    ViewBag.ErrorTitle = $"Cantidad requerida de tipos seleccionados para crear Pokemon {pokemon.Name}";
                    ViewBag.ErrorMessage = $"Para crear pokemon {pokemon.Name} solo debe seleccionar entre 1 y 2 tipos" +
                        $". Seleccione y luego trate de nuevo" +
                        $".";
                    return View("Error1");
                }
                string uniqueFileName = ProcessUploadedFile(pokemon);
                Pokemon pokemonnew = new Pokemon()
                {
                    Name=pokemon.Name,
                    Description=pokemon.Description,
                    Gender= pokemon.Gender,
                    Height=pokemon.Height,
                    Weight=pokemon.Weight,
                    RegionId=pokemon.RegionId,
                    PhotoPath= uniqueFileName

                };
                _context.Add(pokemonnew);
                await _context.SaveChangesAsync();

                foreach (var item in pokemon.AttackIds)
                {
                    PokemonAttack pokemonAttack = new PokemonAttack()
                    {
                        PokemonId = pokemonnew.Id,
                        AttackId = item
                    };
                    _context.Add(pokemonAttack);
                    await _context.SaveChangesAsync();
                }

                foreach (var item in pokemon.TypesIds)
                {
                    PokemonType pokemonType = new PokemonType()
                    {
                        PokemonId = pokemonnew.Id,
                        TypeId = item
                    };
                    _context.Add(pokemonType);
                    await _context.SaveChangesAsync();
                }
                
                return RedirectToAction(nameof(Index));
            }
            ViewData["RegionId"] = new SelectList(_context.Region, "Id", "ClassType", pokemon.RegionId);
            ViewData["TypesId"] = new SelectList(_context.Types, "Id", "Name");
            ViewData["AttackId"] = new SelectList(_context.Attack, "Id", "Name");
            return View(pokemon);
        }