asp.net-mvc 模型不适用 DataType.Password

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6044922/
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-08 01:12:37  来源:igfitidea点击:

Model does not apply DataType.Password

asp.net-mvcasp.net-mvc-3viewmodel

提问by balexandre

Instead of using the object directly, on a simple Razor View I have a form using as it+s modela decorator object.

在一个简单的 Razor 视图上,我没有直接使用该对象,而是使用了一个表单作为model装饰器对象。

public class GlobalAccount
{
    public GlobalAccount()
    {
        this.TestService = new TestServiceModel();
    }

    public TestServiceModel TestService { get; set; }
}

beeing TestServiceModelrepresented as

TestServiceModel表示为

public class TestServiceModel
{
    [Required]
    [Display(Name = "Endpoint (url of your service like http://mydomain/remote/)")]
    public string Endpoint { get; set; }

    [Required]
    [Display(Name = "System username")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "System password")]
    public string Password { get; set; }
}

Notethe Passwordproperty decorated with DataType.Password

请注意Password装饰有的属性DataType.Password

and in a Partial Razor viewI have a call to that object

在一个Partial Razor view我有一个对那个对象的调用

@model OnlineServices.Administration.Models.GlobalAccount
...
@Html.TextBoxFor(model => model.TestService.Password, new { size = "30" })

problem is that, in the htmlI get type="text"instead of text="password"

问题是,在html我得到type="text"而不是text="password"

You can see in this imagethe entire flow:

您可以在此图像中看到整个流程:

enter image description here

在此处输入图片说明

What am I missinghere?

我在这里缺少什么?

回答by Kieron

You should use Html.Password, or even better use Html.EditorForwhich will read the DataType.Passwordand output a password type input for you.

您应该使用Html.Password,或者更好的使用方式Html.EditorFor,它将读取DataType.Password并为您输出密码类型输入。

The Html.TextBoxForis just text based input, not as password based one.

Html.TextBoxFor只是基于文本的输入,而不是基于密码的输入。

回答by Skuld

Further to Kieron's answer, in MVC3 it is actually NOT better to use Html.EditorFor for the Password inputs, as for some reason, if the server returns the page (say the username password combo is incorrect) then with EditorFor, the password is transmitted back to the page (and a view source the password is visible)

进一步 Kieron 的回答,在 MVC3 中使用 Html.EditorFor 作为密码输入实际上并不是更好,因为出于某种原因,如果服务器返回页面(假设用户名密码组合不正确)然后使用 EditorFor,则传输密码返回页面(以及密码可见的查看源)

When using the Html.PasswordFor the password is not transmitted back to the client.

使用 Html.PasswordFor 时,密码不会传回客户端。