模型不适用 DataType.Password

发布于 2024-11-08 01:07:52 字数 1418 浏览 0 评论 0 原文

在一个简单的 Razor 视图上,我没有直接使用该对象,而是使用一个表单作为 it+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; }
}

Note 使用 DataType.Password 修饰的 Password 属性

,并位于 Partial Razor 视图中 我调用了该对象

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

,问题是,在 html 中,我得到 type="text" 而不是 text="password"< /code>

你可以在此图片整个流程:

在此处输入图像描述

我在这里缺少什么?

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:

enter image description here

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-11-15 01:07:52

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

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

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.

青芜 2024-11-15 01:07:52

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文