Beispiel #1
0
        public ActionResult Detail(int Id)
        {
            IngredientIndex ingredient = GetDbSetIngredient().FirstOrDefault(i => i.Id == Id);

            if (ingredient == null)
            {
                return(new HttpNotFoundResult("Ingrédient non trouvé"));
            }

            return(View(ingredient));
        }
Beispiel #2
0
        public ActionResult Delete(int Id)
        {
            IngredientIndex ingredient = _ctx.IngredientIndex.FirstOrDefault(m => m.Id == Id);

            if (ingredient == null)
            {
                return(HttpNotFound());
            }

            _ctx.IngredientIndex.Remove(ingredient);
            _ctx.SaveChanges();
            TempData["MessageSuccess"] = "L'ingrédient a bien été supprimé.";
            return(RedirectToAction("Index"));
        }
Beispiel #3
0
        public ActionResult Edit(int Id)
        {
            IngredientIndex ingredient = _ctx.IngredientIndex.FirstOrDefault(m => m.Id == Id);

            if (ingredient == null)
            {
                return(HttpNotFound());
            }

            IngredientFormViewModel ingredientFormViewModel = new IngredientFormViewModel()
            {
                Ingredient = new IngredientIndex()
            };

            ingredientFormViewModel.Ingredient.Id          = ingredient.Id;
            ingredientFormViewModel.Ingredient.Name        = ingredient.Name;
            ingredientFormViewModel.Ingredient.Description = ingredient.Description;

            return(View("IngredientForm", ingredientFormViewModel));
        }
Beispiel #4
0
        public ActionResult AddOrUpdate(IngredientIndex ingredient)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.MessageError = "Il y a des erreurs dans le formulaire";
                return(View("IngredientForm", new IngredientFormViewModel()
                {
                    Ingredient = ingredient
                }));
            }

            if (ingredient.Id > 0)
            {
                // Màj de l'ingrédient
                IngredientIndex ingredientFromDb = _ctx.IngredientIndex.FirstOrDefault(m => m.Id == ingredient.Id);

                if (ingredientFromDb == null)
                {
                    return(HttpNotFound());
                }

                ingredientFromDb.Name        = ingredient.Name;
                ingredientFromDb.Description = ingredient.Description;
                _ctx.SaveChanges();
                TempData["MessageSuccess"] = "L'ingrédient a bien été modifié";
            }
            else
            {
                // Ajout d'un ingrédient
                _ctx.IngredientIndex.Add(ingredient);
                _ctx.SaveChanges();
                TempData["MessageSuccess"] = "L'ingrédient a bien été ajouté";
            }

            return(RedirectToAction("Index"));
        }