public ActionResult Edit(EmpTable postedEmp) { //get the Entity FM var com = new EmpDataEntities(); //Find UR EMployee var emp = com.EmpTables.FirstOrDefault(e => e.EmpID == postedEmp.EmpID); if (emp == null) { throw new Exception("Employee not found"); } //SEt the new details emp.EmpName = postedEmp.EmpName; emp.EmpAddress = postedEmp.EmpAddress; emp.EmpSalary = postedEmp.EmpSalary; com.SaveChanges(); //update return(RedirectToAction("Index")); }
public HttpResponseMessage Post([FromBody] Emp emp) { try { using (EmpDataEntities entities = new EmpDataEntities()) { entities.Emps.Add(emp); entities.SaveChanges(); var msg = Request.CreateResponse(HttpStatusCode.Created, emp); msg.Headers.Location = new Uri(Request.RequestUri + emp.ID.ToString()); return(msg); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Delete(int id) { try { using (EmpDataEntities entities = new EmpDataEntities()) { var entity = entities.Emps.FirstOrDefault(e => e.ID == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id : " + id.ToString() + "not found to delete")); } else { entities.Emps.Remove(entity); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }