public IHttpActionResult PutTeacher(int id, Teacher teacher) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != teacher.Id) { return(BadRequest()); } db.Entry(teacher).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!TeacherExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "StudentsID,CoursesID")] StudentCours stuc) { if (ModelState.IsValid) { db.StudentCourses.Add(stuc); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(stuc)); }
public ActionResult Create([Bind(Include = "StudeneID,StudentName")] student students) { if (ModelState.IsValid) { db.students.Add(students); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(students)); }
public ActionResult Create([Bind(Include = "CoursesID,CourseName,CourseDesc")] Cours course) { if (ModelState.IsValid) { db.Courses.Add(course); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(course)); }
//Deletes student data from database public IHttpActionResult Delete(int id) { if (id <= 0) { return(BadRequest("Not a valid student Id")); } using (var ctx = new SchoolEntities1()) { var existingStudent = ctx.Students .Where(s => s.StudentID == id) .FirstOrDefault(); if (existingStudent != null) { ctx.Students.Remove(existingStudent); ctx.SaveChanges(); } else { return(NotFound()); } } return(Ok()); }
//Updates the existing data in the database public IHttpActionResult PutStudent(StudentViewModel studentAddress) { if (!ModelState.IsValid) { return(BadRequest("Invalid data")); } using (var ctx = new SchoolEntities1()) { var existingStudent = ctx.Students .Where(s => s.StudentID == studentAddress.Id) .FirstOrDefault(); if (existingStudent != null) { existingStudent.StudentName = studentAddress.StudentName; existingStudent.StandardId = studentAddress.stardardId; existingStudent.StudentAddress.Address1 = studentAddress.Address.Address1; existingStudent.StudentAddress.Address2 = studentAddress.Address.Address2; existingStudent.StudentAddress.City = studentAddress.Address.City; existingStudent.StudentAddress.State = studentAddress.Address.State; ctx.SaveChanges(); } else { NotFound(); } } return(Ok()); }
//Posts new student in the datbase public IHttpActionResult PostNewStudent(StudentViewModel studentAddress) { //This will make sure that the student object includes all the necessary information. If it is not valid, it will return BadRequest response. if (!ModelState.IsValid) { return(BadRequest("Invalid data")); } using (var ctx = new SchoolEntities1()) { ctx.Students.Add(new Student() { StudentID = studentAddress.Id, StudentName = studentAddress.StudentName, StandardId = studentAddress.stardardId, }); ctx.StudentAddresses.Add(new StudentAddress() { StudentID = studentAddress.Address.Id, Address1 = studentAddress.Address.Address1, Address2 = studentAddress.Address.Address2, City = studentAddress.Address.City, State = studentAddress.Address.State, }); ctx.SaveChanges(); } return(Ok()); }