.NET MVC屏蔽密码文本框

ewa*_*ner 12 .net asp.net-mvc

我的印象是使用这样的注释:

<Required()>
<DisplayName("Choose a Password:")>
<ValidatePasswordLength()>
<DataType(DataType.Password)>
Public Property Password As String
Run Code Online (Sandbox Code Playgroud)

在视图中使用时会创建一个蒙版字段:

<%: Html.TextBoxFor(Function(model) model.Password) %>
<%: Html.ValidationMessageFor(Function(model) model.Password) %>
Run Code Online (Sandbox Code Playgroud)

但是这是在没有type ="password"的情况下呈现的

什么是"DataType.Password",如果不是这个?

BC.*_*BC. 26

TextBoxFor覆盖注释,因为它表示明文输入.正如马克林德所提到的,EditorFor尊重注释或者你可以PasswordFor用于那个领域.


mar*_*ind 19

您正在使用Html.TextBoxFor哪个始终<input type="text">在生成的HTML中输出标记.如果您希望自动遵守DataType.Password注释,则应使用Html.EditorFor.您可以在此处阅读有关编辑器模板以及如何自定义它们的更多信息:http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html