예제 #1
0
 private FoodItemViewModel GetFoodItemVM(FoodItem foodItemModel)
 {
     var fivm = new FoodItemViewModel();
     fivm.FoodItem = foodItemModel;
     fivm.AllHealthInfos = db.HealthInfos.ToList(); //CacheManager.GetHealthInfos();
     fivm.FoodItem.Prices.Add(new Price());
     fivm.PriceDisplay = fivm.FoodItem.Prices.Select(p =>
     {
         switch (p.Unit)
         {
             case UnitEnum.Dozen:
                 return $"{p.Amount:C} per dozen";
             case UnitEnum.Each:
                 return $"{p.Amount:C} each";
             case UnitEnum.Pound:
                 return $"{p.Amount:C} per pound";
             default:
                 return $"{p.Amount:C}";
         }
     });
     return fivm;
 }
예제 #2
0
        public ActionResult Edit(FoodItemViewModel fivm)
        {
            if (ModelState.IsValid)
            {
                db.Entry(fivm.FoodItem).State = EntityState.Modified;
                db.Entry(fivm.FoodItem).Collection(fi => fi.HealthInfos).Load();
                fivm.PostedHealthInfoIds.ForEach(i =>
                {
                    if (fivm.FoodItem.HealthInfos.All(h => h.ID != i))
                    {
                        var newHi = new HealthInfo {ID = i};
                        db.HealthInfos.Attach(newHi);
                        fivm.FoodItem.HealthInfos.Add(newHi);//db.HealthInfos.Find(i));

                    }
                });

                fivm.FoodItem.Prices = fivm.FoodItem.Prices.Where(p => p.Amount != decimal.Zero).ToList();

                var newPrice = fivm.FoodItem.Prices.Where(p => p.ID == 0);
                newPrice.ForEach(np => db.Prices.Add(np));

                //db.FoodItems.Attach(fivm.FoodItem);

                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(fivm);
        }