I can do this easily using a TextBoxFor but how do I do it with an EditorFor?
I figured using the DataAnnotation [DataType(DataType.EmailAddress)] but that doesn’t do it.
I don’t quite understand what the DataType annotation actually does because it doesn’t seem to do anything at all at first glance.
The
EditorForhelper method is somewhat limited out of the box and doesn’t yet appear to support the HTML5type="email"attribute.Your options right now seem to be either using
TextBoxForor creating a custom template that will allow you to set the input’stypeattribute. Here’s another thread that reviews some of the options for creating your own templates.The
DataAnnotation [DataType(DataType.EmailAddress)]is actually quite useful. It sets theidandnameof your form field toemail, which you can use with jQuery validation to show the user client-side validation messages. Applying theDataAnnotationto your model class also means that the email property on your model will be automatically validated on the server side. If you enable unobtrusive validation in your app, you get client- and servers-side validation almost for free.