public ActionResult Create(Blog blog) { if (ModelState.IsValid) { var isPosted = _blogRepository.Post(blog); if (isPosted) return RedirectToAction("Index"); } return View(blog); }
public bool Put(Blog blog) { try { _db.Entry(blog).State = EntityState.Modified; _db.SaveChanges(); return true; } catch (Exception) { return false; } }
public bool Post(Blog blog) { try { _db.Blogs.Add(blog); _db.SaveChanges(); return true; } catch (Exception) { return false; } }
// POST api/Blog public HttpResponseMessage PostBlog(Blog blog) { if (ModelState.IsValid) { db.Blogs.Add(blog); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, blog); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = blog.Id })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
// PUT api/Blog/5 public HttpResponseMessage PutBlog(int id, Blog blog) { if (ModelState.IsValid && id == blog.Id) { db.Entry(blog).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.NotFound); } return Request.CreateResponse(HttpStatusCode.OK, blog); } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
public ActionResult Edit(Blog blog) { if (ModelState.IsValid) { var isPut = _blogRepository.Put(blog); if (isPut) return RedirectToAction("Index"); } return View(blog); }