Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(KitchenVM viewmodel)
        {
            if (viewmodel.Recipe == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    Recipe recipe = viewmodel.Recipe;
                    foreach (int element in viewmodel.SelectedProducts)
                    {
                        var recipeProduct = new RecipeProduct
                        {
                            RecipeId  = recipe.Id,
                            ProductId = Convert.ToInt32(element)
                        };
                        if (_context.RecipeProducts.Find(recipeProduct.RecipeId, recipeProduct.ProductId) == null)
                        {
                            _context.Add(recipeProduct);
                        }
                    }
                    _context.Update(recipe);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RecipeExists(viewmodel.Recipe.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(viewmodel));
        }
Ejemplo n.º 2
0
        // GET: Kitchen/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var recipe = await _context.Recipe.FindAsync(id);

            if (recipe == null)
            {
                return(NotFound());
            }
            _context.Entry(recipe).Collection(rp => rp.RecipeProducts).Load();
            var viewmodel = new KitchenVM
            {
                Recipe           = recipe,
                ProductList      = new SelectList(_context.Product, "Id", "Title"),
                SelectedProducts = recipe.RecipeProducts.Select(pr => pr.ProductId)
            };

            return(View(viewmodel));
        }