public ActionResult Index(FormCollection studentData)
        {
            // validate all form data

            // encapsulate form data in an object
            // HelloWorldMVC.Models.Student     or following
            // Models.Student stu = new Models.Student();  or the  following
            // with "using HelloWorldMVC.Models;"
            Student stu = new Student
            {
                StudentId   = studentData["sid"],
                FirstName   = studentData["fName"],
                LastName    = studentData["lName"],
                DateOfBirth = Convert.ToDateTime(studentData["dob"])
            };


            // if everything is valid, add to database
            if (StudentDB.AddStudent(stu))
            {
                // viewbag data only works for the current request
                ViewBag.StudentAdded = true;
            }

            // let the user know it was successful or Display error message(s)
            return(View());
        }
Beispiel #2
0
        public ActionResult Create(Student student)
        {
            if (ModelState.IsValid)
            {
                StudentDB.AddStudent(db, student);
                return(RedirectToAction("Index"));
            }

            return(View(student));
        }
Beispiel #3
0
 public ActionResult Create(Student stu)
 {
     // this ensures all validation rules for the model have passed
     if (ModelState.IsValid)
     {
         if (StudentDB.AddStudent(stu))
         {
             ViewBag.Added = true;
         }
     }
     return(View());
 }
        public ActionResult Index(FormCollection studentData)  //formCollection has all the data from the form so we pass it as a parameter
        {
            //1. Validate all form data

            //2. Encapsulate form data in object | we added a Using so we don't have to code Models.Student. We used the lightbulb to refactor
            //Using HelloWorldMVC.Models
            Student stu = new Student
            {
                StudentId   = studentData["sid"],
                FirstName   = studentData["firstName"],
                LastName    = studentData["lastName"],
                DateOfBirth = Convert.ToDateTime(studentData["dob"])
            };

            //3. If everything is valid, add to database
            if (StudentDB.AddStudent(stu))

            {   //ViewBag data only works for the current request
                ViewBag.StudentAdded = true;
            }

            //4. let the user know it was successful OR display error messages
            return(View());
        }
Beispiel #5
0
        public ActionResult Index(FormCollection data)
        {
            //Validate all form data

            //encapsulate form data in an object
            Student stu = new Student
            {
                StudentId   = data["txtSID"],
                FirstName   = data["txtFName"],
                LastName    = data["txtLName"],
                DateOfBirth = Convert.ToDateTime(data["txtDOB"])
            };

            //if everything is valid, add to database
            if (StudentDB.AddStudent(stu))
            {
                //ViewBag data only works for the current request
                ViewBag.StudentAdded = true;
            }
            //let the user know it was successful
            //Or display error messages

            return(View());
        }