public ActionResult Create(NutritionCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var service = CreateNutritionService();

            if (service.NutritionCreate(model))
            {
                TempData["SaveResult"] = "Your entries have been saved.";
                return(RedirectToAction("Index"));
            }
            ;

            ModelState.AddModelError("", "Could not be created.");

            return(View(model));
        }
示例#2
0
        public bool NutritionCreate(NutritionCreate model)
        {
            var entity =
                new NutritionGuide()
            {
                UserId        = _userId,
                Breakfeast    = model.Breakfast,
                Lunch         = model.Lunch,
                Dinner        = model.Dinner,
                CaloriesFB    = model.Calories_from_Breakfast,
                CaloriesFD    = model.Calories_from_Dinner,
                CaloriesFL    = model.Calories_from_Lunch,
                Weight        = model.Weight,
                TotalCalories = model.Calories_from_Breakfast + model.Calories_from_Dinner + model.Calories_from_Lunch,
                Date          = DateTimeOffset.Now
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.Nutrition.Add(entity);
                return(ctx.SaveChanges() == 1);
            }
        }