我遇到了一个无法解决问题的问题 我在模型中进行了远程验证,如下所示:
[Required]
[Display(Name = "Social Security Number:")]
[Remote("IsSocialSecurityNumberValid", "Applicant", ErrorMessage = "Invalid.")]
public string SocialSecurityNumber { get; set; }
但我还想申请另一项验证,即:
[Remote("SocialSecurityNumberExists", "Applicant", ErrorMessage = "Already exists.")]
但是mvc不允许我添加两个远程属性。 我怎么能解决这个问题呢? 谢谢你的帮助。
答案 0 :(得分:12)
见下面的例子:
[Required]
[Display(Name = "Social Security Number:")]
[Remote("ValidSocialSecurityNumber", "Applicant")]
public string SocialSecurityNumber { get; set; }
你的行动
public JsonResult ValidSocialSecurityNumber([Bind(Prefix = "SocialSecurityNumber ")] string ssn)
{
if (!isSocialSecurityNumberValid)
{
return Json("Invalid.", JsonRequestBehavior.AllowGet);
}
if (isSocialSecurityNumberExists)
{
return Json("Already exists.", JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}