Giordano Giuliano
Giordano Giuliano

Reputation: 757

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.

Upvotes: 4

Views: 6702

Answers (1)

Lin
Lin

Reputation: 15188

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);
}

Upvotes: 13

Related Questions