Reputation: 28064
I have a string property on my model object called EmailAddress
. I'm using Html.EditorFor
to render input fields for all string properties on this page, and a custom editor template which works fine. However, the minute I add the EmailAddressAttribute
to this property, EditorFor appears to no longer detect the property as a string type, or at least, it refuses to use the editor template I have for strings.
This works:
[DisplayName("EmailAddress")]
[Required(ErrorMessage = "Required")]
[StringLength(100, ErrorMessage = "Must be <= 100 characters.")]
public string EmailAddress { get; set; }
// and then in my view...
@Html.EditorFor(x => x.EmailAddress)
This does not:
[DisplayName("EmailAddress")]
[Required(ErrorMessage = "Required")]
[StringLength(100, ErrorMessage = "Must be <= 100 characters.")]
[EmailAddress(ErrorMessage="Invalid email address.")]
public string EmailAddress { get; set; }
// and then in my view...
@Html.EditorFor(x => x.EmailAddress)
In the second example, the default string editor template is used instead of my own. Other properties on the same model render using the correct editor template.
Am I missing something obvious or does this seem like a bug?
Upvotes: 2
Views: 890
Reputation: 887777
The [EmailAddress]
attribute sets the property's DataType to EmailAddress
, which causes EditorFor()
to look for an EmailAddress
template.
Upvotes: 3