public class StudentModel { [Display(Name="学生编号")] public int StuId { set; get; } [Required(ErrorMessage="请输入学生姓名!")] [StringLength(10,ErrorMessage="姓名长度不能长于10!")] [Display(Name="学生姓名")] [Remote("ValirStuName","Student",HttpMethod="Post",ErrorMessage="学生姓名已经存在!")] public string StuName { set; get; } [Display(Name="学生Email")] [DataType(DataType.EmailAddress)] public string StuSex { set; get; } [Display(Name="班级ID")] public int Gid { set; get; } [Display(Name="学生描述")] public string StuDesc { set; get; } }
Controller:
/// <summary> /// 参数名称与Model中的名称一致 /// </summary> /// <param name="StuName"></param> /// <returns></returns> public ActionResult ValirStuName(string StuName) { return Json(true); //return Json("你所填写的用户名,已经存在了!");//当返回不为true时,则默认认为验证失败及自定义的错误信息 //return new JsonResult { Data=false,JsonRequestBehavior=JsonRequestBehavior.AllowGet }; }