public ActionResult Create(Announcement announcement) { if (ModelState.IsValid) { db.Announcements.Add(announcement); db.SaveChanges(); return RedirectToAction("Index"); } return View(announcement); }
// POST api/Ann public HttpResponseMessage PostAnnouncement(Announcement announcement) { if (ModelState.IsValid) { db.Announcements.Add(announcement); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, announcement); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = announcement.annId })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/Ann/5 public HttpResponseMessage PutAnnouncement(string id, Announcement announcement) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != announcement.annId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(announcement).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
public ActionResult Edit(Announcement announcement) { if (ModelState.IsValid) { db.Entry(announcement).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(announcement); }