示例#1
0
        public ActionResult Create(Student std)
        {
            if (!ModelState.IsValid)
            {
                return(View(std));
            }

            if (std == null)
            {
                ModelState.AddModelError(string.Empty, "Student is NULL.");
                return(View(std));
            }

            std.StudentName = std.StudentName.Trim();

            // check whether name already exists in database.
            bool nameAlreadyExists = StudentDao.StudentNameExists(std.StudentName);

            if (nameAlreadyExists)
            {
                ModelState.AddModelError(string.Empty, "Student Name already exists.");
                return(View(std));
            }

            // Update database.
            int i = StudentDao.InsertRecord(std);

            if (i == 1)
            {
                return(RedirectToAction("Index"));
            }
            else if (i < 1)
            {
                ModelState.AddModelError(string.Empty, "Fail to create it in database.");
                return(View(std));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "More than ONE record are created in database.");
                return(View(std));
            }
        }