MVC附带了
RemoteAttribute它,它在内部对控制器方法进行ajax调用,该方法返回一个Json值,该值指示验证是成功还是失败
public JsonResult IsValid(string SourceDirectory){ if (someCondition) //test if the value of SourceDirectory is valid { return Json(true, JsonRequestBehavior.AllowGet); // indicates its valid } else { return Json(false, JsonRequestBehavior.AllowGet); // indicates its not valid // or return Json("A custom error message that overrides the default message defined in the attribute"); }}并用
[Remote("IsValid", "YourController", ErrorMessage = "The path is not valid")]public string SourceDirectory { get; set; }注意:
RemoteAttribute仅适用于客户端(jquery非侵入式验证),您可能仍需要其他服务器端验证。
有关详细示例,请参阅如何:在ASP.NET MVC中实现远程验证。



