//[Authorize] public async Task <ActionResult> CreateItem(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } MenuItem menuItem = await db.MenuItems .Where(x => x.Id == id) .Include(x => x.AdditionalIngredients) .FirstOrDefaultAsync(); if (menuItem == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } List <SelectedIngredient> selectedIngredients = new List <SelectedIngredient>(); foreach (var ingr in menuItem.AdditionalIngredients) { SelectedIngredient selectedIngr = new SelectedIngredient { Name = ingr.Name, Price = ingr.Price, Weight = ingr.Weight, Quantity = 0 }; selectedIngredients.Add(selectedIngr); } return(View(selectedIngredients)); }
public void RemoveIngredient() { var products = (IEnumerable <ComboData>)Ingredients.SourceCollection; var updateProducts = products.Where(p => p.Value != SelectedIngredient); var productsComboData = new List <ComboData>(); var ingredientName = SelectedIngredient.Split(',').FirstOrDefault().Trim(); if (_ingredientsToSave.Count > 0) { var ingredientToSave = _ingredientsToSave.FirstOrDefault(its => its.Name == ingredientName); _ingredientsToSave.Remove(ingredientToSave); } if (Formula.FormulaIngredients.Count > 0) { var formulaToDelete = Formula.FormulaIngredients.Where(fi => fi.Ingredient.Name == ingredientName).ToList(); _context.FormulaIngredients.RemoveRange(formulaToDelete); } if (updateProducts.Count() == 1) { MessageBox.Show("You cannot remove all of the products. ", "Exception", MessageBoxButton.OK, MessageBoxImage.Warning); return; } var formulaToChange = _context.Formulas.Include(f => f.FormulaIngredients.Select(fi => fi.Ingredient)).FirstOrDefault(f => f.Id == _formulaId); foreach (var product in updateProducts) { productsComboData.Add(new ComboData { Value = product.Value }); } Ingredients = new CollectionView(productsComboData); RaisePropertyChanged("Ingredients"); }
/// <summary> /// The drop event /// </summary> private void list_DragDrop(object sender, DragEventArgs e) { List<string> staples = new List<string>(); if (e.Data.GetDataPresent("dragMealFormat")) { foreach (var row in staplesGrid.Items) { XmlElement element = row as XmlElement; if (element != null) { staples.Add(element.Attributes["name"].Value); } } // sender is itemsControl // Update the table data int updateIndex = DecideDropTarget(e, (ItemsControl)sender); XmlElement xElement = e.Data.GetData("dragMealFormat") as XmlElement; SelectedMealCollection colData = (SelectedMealCollection)this.FindResource("SelectedMealCollectionData"); SelectedMeal mealToUpdate = colData[updateIndex]; mealToUpdate.Meal = xElement.GetAttribute("name"); if (mealToUpdate.Ingredients == null) { mealToUpdate.Ingredients = new List<string>(); } else { mealToUpdate.Ingredients.Clear(); } foreach (XmlNode ingredientNode in xElement.SelectNodes("Ingredients/Ingredient")) { mealToUpdate.Ingredients.Add(ingredientNode.InnerText); } // Refresh the selected ingredient view SelectedIngredientsCollection ingredientData = (SelectedIngredientsCollection)this.FindResource("SelectedIngredientsCollectionData"); ingredientData.Clear(); foreach (SelectedMeal meal in colData) { if (meal.Ingredients == null) {continue;} foreach (string ingredient in meal.Ingredients) { SelectedIngredient selectedIngredient = new SelectedIngredient(ingredient); ingredientData.Add(selectedIngredient); } } foreach (string staple in staples) { SelectedIngredient selectedStaple = new SelectedIngredient(staple); ingredientData.Add(selectedStaple); } } index = -1; }