.NET MVC 屏蔽密码文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3258583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:31:26  来源:igfitidea点击:

.NET MVC Masking Password textboxes

.netasp.net-mvc

提问by ewahner

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"

然而,这是在没有 type="password" 的情况下呈现的

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

如果不是这个,“DataType.Password”是做什么用的?

回答by BC.

TextBoxForoverrides your annotation because it indicates a clear text input. As marcind mentioned, EditorForhonors the annotation or you could use PasswordForfor that field.

TextBoxFor覆盖您的注释,因为它指示明文输入。正如 marcind 所提到的,EditorFor尊重注释或者您可以PasswordFor用于该领域。

回答by marcind

You are using Html.TextBoxForwhich 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.EditorForinstead. 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

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