Model不应用DataType.Password

时间:2011-05-18 12:50:39

标签: asp.net-mvc asp.net-mvc-3 viewmodel

而不是直接使用对象,在一个简单的Razor View上我有一个表单,使用+ s model装饰对象。

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

    public TestServiceModel TestService { get; set; }
}

beeing 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; }
}

注意Password

修饰的DataType.Password属性

并在Partial Razor view中我调用了该对象

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

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

您可以在this image中看到整个流程:

enter image description here

  

缺少什么

2 个答案:

答案 0 :(得分:20)

您应该使用Html.Password,或者更好地使用Html.EditorFor来阅读DataType.Password并为您输出密码类型输入。

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

答案 1 :(得分:14)

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

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