public ActionResult Create(Employee employee) { if (ModelState.IsValid) { db.Employees.Add(employee); db.SaveChanges(); return RedirectToAction("Index"); } return View(employee); }
// POST api/HomeApi public HttpResponseMessage PostEmployee(Employee employee) { if (ModelState.IsValid) { db.Employees.Add(employee); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, employee); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = employee.EmployeeId })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
// PUT api/HomeApi/5 public HttpResponseMessage PutEmployee(int id, Employee employee) { if (ModelState.IsValid && id == employee.EmployeeId) { db.Entry(employee).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.NotFound); } return Request.CreateResponse(HttpStatusCode.OK); } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
public ActionResult Edit(Employee employee) { if (ModelState.IsValid) { db.Entry(employee).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(employee); }