public ActionResult Delete(int id) { using (var db = new StudentTestApiContext()) { var stud = db.Students.Where(x => x.Id == id).FirstOrDefault(); return(View(stud)); } }
// POST api/values public void Post([FromBody] Student student) { using (StudentTestApiContext db = new StudentTestApiContext()) { if (student != null) { db.Students.Add(student); db.SaveChanges(); } } }
public ActionResult Delete(Student stud) { using (var db = new StudentTestApiContext()) { var student = db.Students.Where(x => x.Id == stud.Id).FirstOrDefault(); if (student != null) { db.Students.Remove(student); db.SaveChanges(); return(RedirectToAction("AllStudents")); } return(HttpNotFound($"Could not find a student with Id = {stud.Id}")); } }
// DELETE api/values/5 public IHttpActionResult Delete(int id) { using (var db = new StudentTestApiContext()) { var stud = db.Students.Where(x => x.Id == id).FirstOrDefault(); if (stud != null) { db.Students.Remove(stud); db.SaveChanges(); return(Redirect("https://localhost:44314/api/Values")); } return(NotFound()); } }
// GET api/values/5 public string Get(int id) { using (StudentTestApiContext db = new StudentTestApiContext()) { var stud = db.Students.Where(x => x.Id == id).FirstOrDefault(); if (stud != null) { return(JsonConvert.SerializeObject(stud)); } else { return(JsonConvert.SerializeObject("Not Found")); } } }
// PUT api/values/5 public IHttpActionResult Put(int id, [FromBody] Student student) { using (var db = new StudentTestApiContext()) { var stud = db.Students.Where(x => x.Id == id).FirstOrDefault(); if (stud != null) { stud.Id = student.Id; stud.Name = student.Name; stud.email = student.email; stud.Degree = student.Degree; db.SaveChanges(); } return(Redirect("https://localhost:44314/api/Values")); } }
public ActionResult Update(Student stud) { using (var db = new StudentTestApiContext()) { var student = db.Students.Where(x => x.Id == stud.Id).FirstOrDefault(); if (student != null) { if (student.email != stud.email) { SendMail(stud.email, stud.ConfirmCode); } student.Name = stud.Name; student.email = stud.email; student.Degree = stud.Degree; db.SaveChanges(); return(RedirectToAction("AllStudents")); } return(HttpNotFound($"Could not update student data with Id = {stud.Id}")); } }
public ActionResult Add(Student stud) { using (var db = new StudentTestApiContext()) { string _code = Request.Form["Code_"].ToString(); int conCode = Convert.ToInt32(_code); if (stud.ConfirmCode == conCode) { if (stud != null) { db.Students.Add(stud); db.SaveChanges(); return(RedirectToAction("AllStudents")); } } return(HttpNotFound()); } }
public ActionResult AllStudents() { var db = new StudentTestApiContext(); return(View(db.Students)); }