public ActionResult Create(MyFirstMVCProject.Models.Student student) { try { // TODO: Add insert logic here //student.StudentId = ++MvcApplication.globalStudentId; //MvcApplication.studentsList.Add(student); //above commented is previous code if (ModelState.IsValid) { if (student == null) { return(HttpNotFound()); } DataAccessLayer.DBContext.CreateStudent(student); } return(RedirectToAction("Index")); } catch (DataException) { ModelState.AddModelError("", "Unable to create student, Try Again!!!"); return(View()); } }
public ActionResult Edit(int id, MyFirstMVCProject.Models.Student student) { try { // TODO: Add update logic here ////Get student info depends on id given //Models.Student std = MvcApplication.studentsList.FirstOrDefault(s => s.StudentId == id); ////Apply changes //std.StudentName = student.StudentName; //std.Age = student.Age; //above commented is previous code if (ModelState.IsValid) { if (student == null) { return(HttpNotFound()); } DataAccessLayer.DBContext.UpdateStudentById(id, student); } //Redirect to index page which shows listing return(RedirectToAction("Index")); } catch (DataException) { ModelState.AddModelError("", "Unable to edit student, Try Again!!!"); return(View()); } }
public ActionResult Delete(int?id, MyFirstMVCProject.Models.Student student) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (student == null) { return(HttpNotFound()); } try { //// TODO: Add delete logic here ////Get student info depends on id given(user choose by clicking) //Models.Student std = MvcApplication.studentsList.FirstOrDefault(s => s.StudentId == id); ////Remove that student from list //MvcApplication.studentsList.Remove(std); //above commented is previous code DataAccessLayer.DBContext.DeleteStudentById(id); //Redirect to index page which shows listing return(RedirectToAction("Index")); } catch (DataException) { ModelState.AddModelError("", "Unable to delete student, Try Again!!!"); return(View()); } }