public ActionResult Index(Student student) { // Grab the course from the application state var course = HttpContext.Application["course"] as Course; // If the model state is valid do stuff // then redirect back to index if (ModelState.IsValid && course != null) { course.AddStudent(student); TempData["message"] = true; return RedirectToAction("Index"); } // Otherwise show some errors return View(course); }
public ActionResult AddStudent(StudentViewModel studentViewModel) { /* Get the course data from the application state */ var course = (Course) HttpContext.Application["course"]; /* Populate Course and Student model with application data */ var studentCourseViewModel = new StudentCourseViewModel { CourseViewModel = new CourseViewModel { Number = course.Number, Name = course.Name, Students = course.Students }, StudentViewModel = new StudentViewModel { Id = studentViewModel.Id, Name = studentViewModel.Name, Grade = studentViewModel.Grade }, }; /* Check if there are errors in the submission */ if (ModelState.IsValid == false) { return View("AddStudent", studentCourseViewModel); } /* Initialize a new student with the submitted data */ var student = new Student { Id = studentViewModel.Id, Name = studentViewModel.Name, Grade = studentViewModel.Grade }; /* Add the student to the course */ course.AddStudent(student); return RedirectToAction("AddStudent", studentCourseViewModel); }
// Add a student to the list public void AddStudent(Student student) { Students.Add(student); }