public ActionResult Create([Bind(Include = "Id,Name,IcNumber,MatricNumber,DateOfBirth")] Student student) { if (ModelState.IsValid) { _db.Students.Add(student); _db.SaveChanges(); return(RedirectToAction("Index")); } return(View(student)); }
public ActionResult DeleteConfirmed(int id) { //var student = _db.Students.Find(id); //_db.Students.Remove(student); using (var db = new AspNetMvcDbContext()) { // Reduce call to DB var student = new Student { Id = id }; db.Entry(student).State = EntityState.Deleted; db.SaveChanges(); return(RedirectToAction("Index")); } }
public ActionResult EditPost(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } using (var db = new AspNetMvcDbContext()) { var student = db.Students.Find(id); // Use TryUpdateModel to update fields from user input in the posted form data // Bind resets fields if (!TryUpdateModel(student, "", new[] { "MatricNumber", "Name", "IcNumber", "DateOfBirth" })) { return(View(student)); } db.SaveChanges(); return(RedirectToAction("Index")); } }