Esempio n. 1
0
        public ActionResult Add(FormCollection form)
        {
            var individualToAdd = new Individual();

            // Deserialize (Include white list!)
            TryUpdateModel(individualToAdd, new string[] { "Name", "DateOfBirth" }, form.ToValueProvider());

            // Validate
            if (String.IsNullOrEmpty(individualToAdd.Name))
                ModelState.AddModelError("Name", "Name is required!");
            if (String.IsNullOrEmpty(individualToAdd.DateOfBirth))
                ModelState.AddModelError("DateOfBirth", "DateOfBirth is required!");

            var error = individualToAdd.ValidateDateOfBirth();
            if (!String.IsNullOrEmpty(error))
                ModelState.AddModelError("DateOfBirth", error);

            // If valid, save Individual to Database
            if (ModelState.IsValid)
            {
                _db.AddToIndividuals(individualToAdd);
                _db.SaveChanges();
                return RedirectToAction("Add");
            }

            // Otherwise, reshow form
            return View(individualToAdd);
        }