1.创建一个验证Action
public JsonResult CheckName(string name) { return Json(!name.Contains("1"), JsonRequestBehavior.AllowGet); }
2.在实体上添加Remote标记
public class UserEntity { [Required] [Remote("CheckName", "User", ErrorMessage = "包含违禁词汇")] public string Name { get; set; } public int? Age { get; set; } }
3.在View中,添加验证代码
public class UserEntity { [Required] [Remote("CheckName", "User", ErrorMessage = "包含违禁词汇")] public string Name { get; set; } public int? Age { get; set; } }
这样,可以完成远程验证功能,非常方便。