public bool UpdateQueen(QueenEdit model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx.Queens.Single(e => e.Id == model.Id); entity.Id = model.Id; entity.Age = model.Age; entity.Color = model.Color; entity.OriginDate = model.OriginDate; entity.OriginLocation = model.OriginLocation; return(ctx.SaveChanges() == 1); } }
// GET: Queen/Edit/5 public ActionResult Edit(int id) { var service = new QueenService(); var detail = service.GetQueenById(id); var model = new QueenEdit { Age = detail.Age, Color = detail.Color, OriginDate = detail.OriginDate, OriginLocation = detail.OriginLocation, }; return(View(model)); }
public ActionResult Edit(int id, QueenEdit model) { if (!ModelState.IsValid) { return(View(model)); } if (model.Id != id) { ModelState.AddModelError("", "Id Mismatch"); return(View(model)); } var service = new QueenService(); if (service.UpdateQueen(model)) { TempData["SaveResult"] = "Your Queen was added. Long live the Queen."; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Your Queen could not be added."); return(View(model)); }