asp.net mvc Remote远程验证

1.Model实体

/// <summary>

        /// 课程编号

        /// </summary>

        [MaxLength(50)]

        [Remote("IsUnique_Available", "Course")]

        [Required(ErrorMessageResourceName = "Course_CourseNum", ErrorMessageResourceType = typeof(ErrorMessage))]

        public string CourseNum { get; set; }

2.CourseController控制器IsUnique_Available验证方法

#region 验证课程编号不唯一

        public JsonResult IsUnique_Available(string CourseNum)

        {

            Course model = o_Res.GetCourseAny(a => a.CourseNum == CourseNum);

            if (model == null)

            {

                return Json(true, JsonRequestBehavior.AllowGet);

            }

            else

            {

                string suggestedUID = String.Format(CultureInfo.InvariantCulture, "课程编号:{0} 已经存在!", CourseNum);

                return Json(suggestedUID, JsonRequestBehavior.AllowGet);

            }

        }

        #endregion

3.cshtml添加页面

<td colspan="3">

                    @Html.TextBoxFor(a => a.CourseNum, new { maxlength = 50 }) @Html.ValidationMessageFor(model => model.CourseNum)

                </td>

4.cshtml编辑页面(disabled = true

<td colspan="3">

                    @Html.TextBoxFor(a => a.CourseNum, new { maxlength = 50 , disabled = true }) @Html.ValidationMessageFor(model => model.CourseNum)

                </td>

 

你可能感兴趣的:(asp.net)