Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET MVC Masking Password textboxes

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?

like image 372
ewahner Avatar asked Jul 15 '10 18:07

ewahner


2 Answers

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

like image 29
marcind Avatar answered Sep 29 '22 08:09

marcind


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.

like image 172
BC. Avatar answered Sep 29 '22 09:09

BC.