public ActionResult Create(Posting posting) { if (!ModelState.IsValid) { TempData["Message"] = "Invalid posting!"; return RedirectToAction("Index"); } _miniSpiirDbContext.Postings.Add(posting); _miniSpiirDbContext.SaveChanges(); return RedirectToAction("Index"); }
// POST api/PostingApi public HttpResponseMessage PostPosting(Posting posting) { if (ModelState.IsValid) { db.Postings.Add(posting); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, posting); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = posting.Id })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
// PUT api/PostingApi/5 public HttpResponseMessage PutPosting(int id, Posting posting) { if (ModelState.IsValid && id == posting.Id) { db.Entry(posting).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.NotFound); } return Request.CreateResponse(HttpStatusCode.OK); } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }