public ActionResult Edit(int id, HappinessEdit model) { if (!ModelState.IsValid) { return(View(model)); } if (model.HappinessId != id) { ModelState.AddModelError("", "Id Mismatch"); return(View(model)); } var service = CreateHappinessService(); if (service.UpdateHappiness(model)) { TempData["SaveResult"] = "Your entry was updated."; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Your entry could not be updated."); ViewBag.PersonId = new SelectList(_db.Persons, "PersonId", "Name", model.PersonId); return(View(model)); }
public bool UpdateHappiness(HappinessEdit model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .Happinesses .Single(e => e.HappinessId == model.HappinessId && e.OwnerId == _userId); entity.HappinessLevel = model.HappinessLevel; entity.EmotionNotes = model.EmotionNotes; entity.Date = model.Date; entity.PersonId = model.PersonId; return(ctx.SaveChanges() == 1); } }
public ActionResult Edit(int id) { var service = CreateHappinessService(); var detail = service.GetHappinessById(id); var model = new HappinessEdit { HappinessId = detail.HappinessId, HappinessLevel = detail.HappinessLevel, EmotionNotes = detail.EmotionNotes, Date = detail.Date, PersonId = detail.PersonId }; ViewBag.PersonId = new SelectList(_db.Persons, "PersonId", "Name", model.PersonId); return(View(model)); }