コード例 #1
0
        public ActionResult MakeBooking(Appointment appointment)
        {
            if (string.IsNullOrWhiteSpace(appointment.ClientName))
                ModelState.AddModelError("ClientName", "Please enter your name [controller]");

            if (ModelState.IsValidField("Date") && DateTime.Now.Date > appointment.Date)
                ModelState.AddModelError("Date", "Please enter a date in the future [controller]");

            if (!appointment.TermsAccepted)
                ModelState.AddModelError("TermsAccepted", "You must accept the terms [controller]");

            if (ModelState.IsValidField("ClientName") && ModelState.IsValidField("Date") &&
                appointment.ClientName == "Joe" && appointment.Date.DayOfWeek == DayOfWeek.Monday)
                ModelState.AddModelError("", "Joe cannot book appointments on Mondays [controller]");

            if (ModelState.IsValid)
            {
                // Save appointment
                // ...

                return View("Completed", appointment);
            }
            else
                return View();
        }
コード例 #2
0
        public ActionResult MakeBooking2(Appointment appointment)
        {
            if (ModelState.IsValid)
            {
                // Save appointment
                // ...

                return View("Completed", appointment);
            }
            else
                return View("MakeBooking");
        }