public async Task <IActionResult> EditIngredients(int id, [FromQuery] EditCocktailViewModel cocktailToEdit) { if (cocktailToEdit.CocktilNewIngredients.Count() == 0 && cocktailToEdit.IngredientsToRemove.Count() == 0) { cocktailToEdit = (await cocktailServices.GetDetailedDTOAsync(id)).MapToEditViewModel(); cocktailToEdit.ListCurrentIngredients = cocktailToEdit.IngredientsQuantity .Select(i => i.Value + i.Unit + " " + i.Name) .ToList(); var ingredients = await cocktailServices.GetAllNotIncludedIngredientsDTOAsync(id); cocktailToEdit.AllNotIncludedIngredients = ingredients .Select(b => new SelectListItem($"{b.Name}, {b.Unit}", $"{b.Name}, {b.Unit}")).ToList(); cocktailToEdit.CurrentCocktailIngredients = cocktailToEdit.IngredientsQuantity .Select(i => new SelectListItem($"{i.Name}, {i.Unit}", i.Name)).ToList(); return(View(cocktailToEdit)); } foreach (var ingr in cocktailToEdit.CocktilNewIngredients) { cocktailToEdit.IngredientsQuantity.Add(new CocktailIngredientViewModel { Name = ingr, Value = 0 }); } if (cocktailToEdit.IngredientsToRemove != null) { foreach (var ingr in cocktailToEdit.IngredientsToRemove) { var iq = cocktailToEdit.IngredientsQuantity.FirstOrDefault(c => c.Name == ingr); cocktailToEdit.IngredientsQuantity.Remove(iq); } } return(View(cocktailToEdit)); }