public ActionResult Create(Location location) { if (ModelState.IsValid) { locationRepository.InsertOrUpdate(location); locationRepository.Save(); return RedirectToAction("Index"); } else { return View(); } }
public void InsertOrUpdate(Location location) { if (location.Id == default(int)) { // New entity context.Locations.Add(location); } else { // Existing entity context.Entry(location).State = EntityState.Modified; } }
// POST api/LocationAPI public HttpResponseMessage PostLocation(Location location) { if (ModelState.IsValid) { locationRepository.InsertOrUpdate(location); locationRepository.Save(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, location); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = location.Id })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
// PUT api/LocationAPI/5 public HttpResponseMessage PutLocation(int id, Location location) { if (ModelState.IsValid && id == location.Id) { try { locationRepository.InsertOrUpdate(location); locationRepository.Save(); } catch { return Request.CreateResponse(HttpStatusCode.NotFound); } return Request.CreateResponse(HttpStatusCode.OK, location); } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }