public ActionResult OnPost() { if (!ModelState.IsValid) { Students = _enrollmentdbcontext.Students.ToList(); return(Page()); } _enrollmentdbcontext.Students.Add(Student); _enrollmentdbcontext.SaveChanges(); return(Redirect("/student")); }
public ActionResult OnPost() { if (!ModelState.IsValid) { return(Page()); } //-------------------------------------- //first approach is to use a generic variable //-------------------------------------- ////get the student to edit //var stud = _enrollmentdbcontext.Students.FirstOrDefault(student => student.StudentId==Student.StudentId); //-------------------------------------- //second approach is to use a generic variable //-------------------------------------- Student stud = new Student(); stud = _enrollmentdbcontext.Students.FirstOrDefault(student => student.StudentId == Student.StudentId); if (stud != null) { //update each field based on the submitted POST values stud.Name = Student.Name; stud.Age = Student.Age; stud.Address = Student.Address; //update the student _enrollmentdbcontext.Update(stud); //save the changes _enrollmentdbcontext.SaveChanges(); } return(Redirect("Student")); }