コード例 #1
0
        public async Task <IActionResult> AddIngredient(EditDishViewModel mdl)
        {
            // Check if the state is valid, see EditDishViewModel for valid attributes
            if (string.IsNullOrWhiteSpace(mdl.NewIngredient))
            {
                mdl.Dish = await _dishService.GetDishAsync(mdl.Dish.MatrattId);

                mdl.Categories = await _dishService.GetDishCategoriesAsync();

                mdl.Ingredients = await _dishService.GetDishIngredientsAsync();

                ModelState.AddModelError("NewIngredient", "Namnet får endast innehålla a-ö.");
                return(PartialView("_AddIngredientPartialView", mdl));
            }

            // Save new ingredient and attach it to the dish
            await _dishService.AddIngredientToDish(mdl.NewIngredient, mdl.Dish.MatrattId);

            // Refill the ViewModel and return
            mdl.Dish = await _dishService.GetDishAsync(mdl.Dish.MatrattId);

            mdl.NewIngredient = string.Empty;
            mdl.Categories    = await _dishService.GetDishCategoriesAsync();

            mdl.Ingredients = await _dishService.GetDishIngredientsAsync();

            return(PartialView("_AddIngredientPartialView", mdl));
        }
コード例 #2
0
        public async Task <IActionResult> AddIngredient(int dishID, int ingredientID)
        {
            var result = await _dishService.AddIngredientToDish(dishID, ingredientID);

            if (result == false)
            {
                ViewBag.IngredientError = "Ingrediensen finns redan i maträtten";
            }

            var model = new EditDishViewModel
            {
                Dish        = _dishService.FetchDish(dishID),
                Ingredients = _dishService.FetchDishIngredients(),
                Categories  = _dishService.FetchDishCategories()
            };

            return(ViewComponent("EditDishIngredients", model));
        }