In my application several models need Password
properties (eg, Registration
and ChangePassword
models). The Password
property has attribute like DataType
and Required
. Therefore to ensure of re-usability an consistency, I created :
interface IPasswordContainer{
[Required(ErrorMessage = "Please specify your password")]
[DataType(DataType.Password)]
string Password { get; set; }
}
And
class RegistrationModel : IPasswordContainer {
public string Password { get; set; }
}
Unfortunately, the attributes does not work.
Then I tried changing the interface to a class:
public class PasswordContainer {
[Required(ErrorMessage = "Please specify your password")]
[DataType(DataType.Password)]
public virtual string Password { get; set; }
}
And
public class RegistrationModel : PasswordContainer {
public override string Password { get; set; }
}
Now it is working. Why it is so?
Why the attributes are working when inherited from class but not working when inherited from interface?