Ejemplo n.º 1
0
        public IActionResult Details()
        {
            var id = int.Parse(this.Request.Path.Value.Split("/").Last());

            Cat cat = this.Context.Cats.Find(id);

            var catModel = new CatBindingModel()
            {
                Age      = cat.Age,
                Name     = cat.Name,
                Breed    = cat.Breed,
                ImageUrl = cat.ImageUrl
            };

            return(View(model: catModel));
        }
Ejemplo n.º 2
0
        public IActionResult Add(CatBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.Redirect("/cats/add"));
            }

            var cat = new Cat()
            {
                Name     = model.Name,
                Age      = model.Age,
                Breed    = model.Breed,
                ImageUrl = model.ImageUrl
            };

            this.context.Cats.Add(cat);
            this.context.SaveChanges();

            return(this.Redirect("/"));
        }
Ejemplo n.º 3
0
        public IActionResult Add(CatBindingModel catBindingModel)
        {
            if (catBindingModel.Name == "" || catBindingModel.Name == null)
            {
                this.ViewData["message"] = "Name cannot be null or empty string!";
                return(this.View());
            }
            else if (catBindingModel.Breed == "" || catBindingModel.Breed == null)
            {
                this.ViewData["message"] = "Breed cannot be null or empty string!";
                return(this.View());
            }
            else if (catBindingModel.Age < 0 || catBindingModel.Age.ToString() == "")
            {
                this.ViewData["message"] = "Age cannot be less than 0!";
                return(this.View());
            }
            else if (catBindingModel.ImageUrl == "" || catBindingModel.ImageUrl == null)
            {
                this.ViewData["message"] = "Image cannot be null or empty string!";
                return(this.View());
            }

            Cat newCat = new Cat()
            {
                Name     = catBindingModel.Name,
                Age      = catBindingModel.Age,
                Breed    = catBindingModel.Breed,
                ImageUrl = catBindingModel.ImageUrl
            };

            this.Context.Cats.Add(newCat);

            this.Context.SaveChanges();

            return(this.RedirectToAction("Details", new { id = newCat.Id }));
        }