public void Update(StudentModel student) { FirstName = student.FirstName; LastName = student.LastName; Email = student.Email; Approved = student.Approved; }
public IHttpActionResult PostStudent(StudentModel student) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var dbStudent = new Student(); dbStudent.Update(student); db.Students.Add(dbStudent); try { db.SaveChanges(); } catch (Exception) { throw new Exception("Unable to add the student to the database."); } student.StudentId = dbStudent.StudentId; return CreatedAtRoute("DefaultApi", new { id = student.StudentId }, student); }
public IHttpActionResult PutStudent(int id, StudentModel student) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != student.StudentId) { return BadRequest(); } var dbStudent = db.Students.Find(id); dbStudent.Update(student); db.Entry(student).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!StudentExists(id)) { return NotFound(); } else { throw new Exception("Unable to update student in the database."); } } return StatusCode(HttpStatusCode.NoContent); }