views:

48

answers:

2

I was under the impression that using an annotation like this:

<Required()>
<DisplayName("Choose a Password:")>
<ValidatePasswordLength()>
<DataType(DataType.Password)>
Public Property Password As String

Would create a masked field when used in the view:

<%: Html.TextBoxFor(Function(model) model.Password) %>
<%: Html.ValidationMessageFor(Function(model) model.Password) %>

However this is rendered without the type="password"

What is the "DataType.Password" used for if not this?

+3  A: 

You are using Html.TextBoxFor which always outputs an <input type="text"> tag in the resulting HTML. If you want the DataType.Password annotation to be automatically honored you should use Html.EditorFor instead. You can read more about editor templates and how to customize them here: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

marcind
By the same token does that apply for fields that are marked as DataType.EmailAddress ?
The DisplayFor method has a default template for EmailAddress. However there is no default editor for emails that EditorFor could use. It will just use a plain text box. You could write a custom editor template, however.
marcind
+1  A: 

TextBoxFor overrides your annotation because it indicates a clear text input. As marcind mentioned, EditorFor honors the annotation or you could use PasswordFor for that field.

BC