示例#1
0
        //public ActionResult Edit([Bind(Include = "StudentId, StudentName")] Student std) // Using this form will only shows StudentId and StudentName.
        public ActionResult Edit(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, std.StudentId);

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

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

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