Esempio n. 1
0
        public IActionResult CrearPokemon(Pokemn pokemn, IFormFile photo)
        {
            List <string> tipos = new List <string>();

            tipos.Add("Agua");
            tipos.Add("Fuego");
            tipos.Add("Planta");
            tipos.Add("Electrico");
            tipos.Add("Roca");

            ViewBag.tipos = tipos;
            validarPokemon(pokemn);
            if (photo == null)
            {
                ModelState.AddModelError("Image", "Ingrese una Imagen");
            }
            if (ModelState.IsValid)
            {
                if (photo.Length > 0)
                {
                    var filePath = Path.Combine(env.WebRootPath, "Images", photo.FileName);
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        photo.CopyTo(stream);
                    }
                }
                pokemn.Image = photo.FileName;
                context.Pokemons.Add(pokemn);
                context.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(pokemn));
        }
Esempio n. 2
0
        public void validarPokemon(Pokemn pokemn)
        {
            if (pokemn.Nombre == null || pokemn.Nombre == "")
            {
                ModelState.AddModelError("Nombre", "El campo Nombre es requerido");
            }
            else
            {
                var obj = context.Pokemons.Where(a => a.Nombre.Equals(pokemn.Nombre)).FirstOrDefault();


                if (obj != null)
                {
                    if (pokemn.Nombre == obj.Nombre.ToString())
                    {
                        ModelState.AddModelError("Nombre1", "Ese nombre de Pokemon ya existe");
                    }
                }
            }
            if (pokemn.Tipo == null || pokemn.Tipo == "")
            {
                ModelState.AddModelError("Tipo", "El campo Tipo es requerido");
            }
        }