Is it safe to self-reference for MetadataType in ASP.NET MVC?
I've seen the [MetadataType(T)]
attribute for MVC, it's all fine and good, but I was wondering if there are any outstanding repercussions of self-referencing the class it is placed on. I've used it, tried it, and it works wonderfully. Too good to be true, in fact. So, what I basically want to know is ...am I doing something dangerous with the following code?
[MetadataType(typeof(RegisterViewModel))]
public class RegisterViewModel : IMember {
[Required]
[DataType(DataType.EmailAddress)]
[RegularExpression(Text.RegularExpressions.Email, ErrorMessage = Text.ErrorMessages.Email)]
[Display(Name = "Email Address")]
[Rules("The name you'll login with. You can't use: <e开发者_如何转开发m>[ ] | ; , $ \\ < > \"</em>")]
public string Email { get; set; }
[Required(ErrorMessage = "You must enter a password.")]
[StringLength(32, MinimumLength = 6)]
[DataType(DataType.Password)]
[RegularExpression(Text.RegularExpressions.Password, ErrorMessage = Text.ErrorMessages.Password)]
[Display(Name = "Enter your password")]
[Rules("Passwords must be between 6 and 32 characters, may contain any alphanumeric character and the symbols <em>@ # $ %</em> only.")]
public string Password { get; set; }
[Required(ErrorMessage = "You must confirm your password.")]
[StringLength(32, MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Re-enter your password")]
[RegularExpression(Text.RegularExpressions.Password, ErrorMessage = Text.ErrorMessages.Password)]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
There's no point.
The [MetadataType]
attribute tells MVC to read attributes from properties on a surrogate class instead of the original properties on the class itself.
It's meant for situations in which you cannot add attributes to the original class (eg, if the class was automatically generated by a designer)
Without this attribute, MVC will read attributes from your class itself.
精彩评论