public bool UpdateVisitor(VisitorEdit model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .Visitors .Single(e => e.VisitorId == model.VisitorId); entity.FirstName = model.FirstName; entity.LastName = model.LastName; return(ctx.SaveChanges() == 1); } }
public ActionResult Edit(int id) { var service = CreateVisitorService(); var detail = service.GetVisitorById(id); var model = new VisitorEdit { VisitorId = detail.VisitorId, FirstName = detail.FirstName, LastName = detail.LastName, FullName = $"{detail.FirstName} {detail.LastName}" }; return(View(model)); }
public IHttpActionResult Put(VisitorEdit model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } VisitorService service = CreateVisitorService(); if (service.UpdateExistingVisitor(model)) { return(Ok()); } return(InternalServerError()); }
public bool UpdateExistingVisitor(VisitorEdit model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx.Visitors.FirstOrDefault(visitor => visitor.VisitorID == model.VisitorID && visitor.OwnerID == _userID); if (entity == null) { return(false); } entity.FirstName = model.FirstName; entity.LastName = model.LastName; return(ctx.SaveChanges() == 1); } }
public ActionResult Edit(int id, VisitorEdit model) { if (!ModelState.IsValid) { return(View(model)); } if (model.VisitorId != id) { ModelState.AddModelError("", "Ids are mismatched."); return(View(model)); } var service = CreateVisitorService(); if (service.UpdateVisitor(model)) { TempData["SaveResult"] = "Visitor was updated."; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Visitor could not be updated."); return(View(model)); }