views:

51

answers:

1

I'm a nub in MVC.I've a Model:

    public class Usuarios
    {

     [Required(ErrorMessage = "**TxtOPID is required")]
        public string TxtOpID
        {
            get { return this.txt_opId; }
            set { this.txt_opId = value; }
        }

     [Required(ErrorMessage="**Password is required")]
        public string TxtPassword
        {
            get { return this.txt_password; }
            set { this.txt_password = value; }
        }

        [Required(ErrorMessage="**Email is required")]
        [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$",ErrorMessage="**Invalid email")]
        public string TxtEmail
        {
            get { return this.txt_email; }
            set { this.txt_email = value; }
        }
}

This is DataAnnotations and works fine when i try to check if all properties are valid with ModelState.IsValid propertie.

The problem is when i dont want to check ALL properties.i.e: If i want to check only TxtOPID and TxtSenha propertie,like in a Login form,where only OPID and Password are required.

How can i exclude Email propertie validation,in a specific Action in a controller?

I tried:

  public ActionResult SignIn([Bind(Exclude="TxtEmail")]Usuarios usuario)
  {
    [...]
  }

But it doesn't work,its always INVALID cause,TxtEmail is not required for that specific form.

Any ideias?

+2  A: 

Don't put all of your validation in a single class. Build a class for Login, and another one for Contact, etc.

Basically each model will have DataAnnotations to validate that model.. even if you have 30 different ones. You can always create a Base Class and put common properties in there and simply inherit from it.

In my situation I have a login form and the Class (using DataAnnotations) validates "UserName" and "Password". I also have an "Events" form that requires the event name, date, time, etc. So I have another Class for validating events.

rockinthesixstring
but,those classes will repeat they properties?
yes, they can... for example. you could use "email" in 3 different places. one for a contact form, one for a login form, one for a gravatar. You would make 3 Models (login, email, gravatar) and validate the email property on each of the models.
rockinthesixstring
they can,but this is not recommeded in OOP
your not validating the overall concept of an "EMAIL", your validating a Model and whatever properties are within that Model. That's the idea of MVC. You build a model, use it in multiple places, and the validation is exactly the same. If email is supposed to be valid in one scenario and not validated in another scenario, then that email property has different contexts whereby it requires different models.
rockinthesixstring
@user257234 - you can always use inheritance and put common properties in some meaningful base class. Also, switch to automatic properties, it will save you typing so much: public string Password { get; set; }
Jakub Konecki
+1 for common properties and automatic properties. - Love .NET4
rockinthesixstring