コード例 #1
0
        public IActionResult Add(KittenAddingModel model)
        {
            using (this.Context)
            {
                var breed = this.Context.Breeds
                            .FirstOrDefault(b => b.Name == model.Breed);
                if (breed == null)
                {
                    this.Model.Data["error"] = "Invalid breed";
                    return(this.View());
                }

                var kitten = new Kitten()
                {
                    Name  = model.Name,
                    Age   = model.Age,
                    Breed = breed
                };

                this.Context.Kittens.Add(kitten);
                this.Context.SaveChanges();

                return(this.RedirectToAction("/kittens/all"));
            }
        }
コード例 #2
0
        public IActionResult Add(KittenAddingModel model)
        {
            if (!this.User.IsAuthenticated)
            {
                return(RedirectToHome());
            }

            if (!this.IsValidModel(model))
            {
                SetValidatorErrors();
                return(this.View());
            }

            Breed breed = this.kittens.GetBreed(model.Breed);

            if (breed == null)
            {
                this.ShowError("Invalid Breed!");
                return(this.View());
            }

            var kitten = this.kittens.Create(model.Name, model.Age, breed);

            if (kitten == null)
            {
                ShowError("Unsuccessfull operation!");
                return(this.View());
            }

            return(RedirectToAction("/kittens/all"));
        }