Monday, July 2, 2012

Controllers - Validation


Remote Attribute

RemoteAttribute provides jQuery Validation plug-in's remote validator support. Client-side validation library automatically calls a custom method that you define on the server in order to perform validation logic that can only be done server-side.

// Model
public class User
{
    [Remote("EmailAddressAvailable", "Users")]
    public string EmailAddress { get; set; }
}

The following example shows the corresponding controller.

public class SomeController
{
    public bool EmailAddressAvailable(string username)
    {
        if(Db.EmailAddressExists(username))
        {
            return false;
        }
        return true;
    }
}