Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple remote validation attribute in ASP.NET MVC4

I've got a problem that I can't figure how to solve.
I have a remote validation in a model, just like this:

[Required]
[Display(Name = "Social Security Number:")]
[Remote("IsSocialSecurityNumberValid", "Applicant", ErrorMessage = "Invalid.")]
public string SocialSecurityNumber { get; set; }

But there's another validation that I would like to apply, that is:

[Remote("SocialSecurityNumberExists", "Applicant", ErrorMessage = "Already exists.")]

But mvc doesn't let me add two remote attributes. How could I solve that?
Thanks for the help.

like image 563
Giordano Giuliano Avatar asked Dec 16 '22 03:12

Giordano Giuliano


1 Answers

See below an example:

    [Required]
    [Display(Name = "Social Security Number:")]
    [Remote("ValidSocialSecurityNumber", "Applicant")]
    public string SocialSecurityNumber { get; set; }

Your Action

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);
}
like image 57
Lin Avatar answered Dec 17 '22 16:12

Lin