// GET: GlucoseTracker/Edit/{id}
        public ActionResult Edit(int id)
        {
            var service = CreateGlucoseTrackerService();
            var detail  = service.GetGlucoseTrackerById(id);
            var model   =
                new GlucoseTrackerEdit
            {
                TrackerId      = detail.TrackerId,
                Date           = detail.Date,
                BloodGlucose   = detail.BloodGlucose,
                CorrectionDose = detail.CorrectionDose,
                TotalCarbs     = detail.TotalCarbs,
                FoodDose       = detail.FoodDose,
                InsulinDose    = detail.InsulinDose,
                TimeOfDose     = detail.TimeOfDose
            };

            return(View(model));
        }
        public bool UpdateGlucoseTracker(GlucoseTrackerEdit glucose)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity =
                    ctx
                    .Glucose
                    .Single(g => g.TrackerId == glucose.TrackerId && g.OwnerId == _userid);

                entity.TrackerId      = glucose.TrackerId;
                entity.Date           = glucose.Date;
                entity.BloodGlucose   = glucose.BloodGlucose;
                entity.CorrectionDose = glucose.CorrectionDose;
                entity.TotalCarbs     = glucose.TotalCarbs;
                entity.FoodDose       = glucose.FoodDose;
                entity.InsulinDose    = glucose.InsulinDose;
                entity.TimeOfDose     = glucose.TimeOfDose;
                entity.ModifiedUtc    = DateTimeOffset.Now;

                return(ctx.SaveChanges() == 1);
            }
        }
        public ActionResult Edit(int id, GlucoseTrackerEdit model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (model.TrackerId != id)
            {
                ModelState.AddModelError("", "Invalid Id");
                return(View(model));
            }

            var service = CreateGlucoseTrackerService();

            if (service.UpdateGlucoseTracker(model))
            {
                TempData["SaveResult"] = "Your data was updated.";
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "Your data could not be updated.");
            return(View(model));
        }