// PUT api/AddressBook/5 public HttpResponseMessage PutContact(int id, Contact contact) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != contact.Id) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(contact).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
public void InsertOrUpdate(Contact contact) { if (contact.Id == default(int)) { // New entity context.Contacts.Add(contact); } else { // Existing entity context.Entry(contact).State = EntityState.Modified; } }
public ActionResult Create(Contact contact) { if (ModelState.IsValid) { contactRepository.InsertOrUpdate(contact); contactRepository.Save(); return RedirectToAction("Index"); } else { return View(); } }
// POST api/AddressBook public HttpResponseMessage PostContact(Contact contact) { if (ModelState.IsValid) { db.Contacts.Add(contact); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, contact); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = contact.Id })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }