Model does not apply DataType.Password

懵懂的女人 提交于 2019-12-30 17:19:12

问题


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

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

    public TestServiceModel TestService { get; set; }
}

beeing TestServiceModel represented as

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

Note the Password property decorated with DataType.Password

and in a Partial Razor view I have a call to that object

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

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

You can see in this image the entire flow:

What am I missing here?


回答1:


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

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




回答2:


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)

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



来源:https://stackoverflow.com/questions/6044922/model-does-not-apply-datatype-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!