Model不应用DataType.Password

bal*_*dre 12 asp.net-mvc viewmodel asp.net-mvc-3

而不是直接使用对象,在一个简单的Razor View上我有一个表单使用它model作为装饰对象.

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

    public TestServiceModel TestService { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

蜜蜂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; }
}
Run Code Online (Sandbox Code Playgroud)

请注意Password装饰的酒店DataType.Password

在一个Partial Razor view我打电话给那个对象

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

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

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

在此输入图像描述

我在这里错过了什么?

Kie*_*ron 21

你应该使用Html.Password,甚至更好地使用Html.EditorFor它来读取DataType.Password和输出密码类型输入.

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

  • 考虑使用Html.PasswordFor(请参阅下面的@Skuld答案) (2认同)

Sku*_*uld 14

进一步根据Kieron的回答,在MVC3中,使用Html.EditorFor作为密码输入实际上并不是更好,因为出于某种原因,如果服务器返回页面(比如用户名密码组合不正确),那么使用EditorFor,密码被传输返回页面(和密码可见的视图源)

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