输入密码 Blazor 表单

mko*_*mko 14 .net blazor

我正在 Blazor 中添加一个表单,我正在按照此处指定的说明进行操作

https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.0

是否可以添加带有密码掩码的输入。

我尝试添加做类似的事情

public class LoginModel
    {
        [Required]
        public string Username { get; set; }

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

但这没有用。有没有办法在输入时隐藏密码?

agu*_*ars 17

您可以使用该InputText组件type=password

<EditForm Model="@model">
    <DataAnnotationsValidator />
    <ValidationSummary />
    <InputText type="password" placeholder="Password" @bind-Value="@model.Password" />
</EditForm>

@code {
    class Login
    {
        [Required]
        public string Password { get; set; }
    }

    private Login model = new Login();
}
Run Code Online (Sandbox Code Playgroud)

InputText支持所有<input />属性

  • 使用 System.ComponentModel.DataAnnotations 中的比较属性 [必需] public string Password { get; 放; [必需] [Compare("密码", ErrorMessage = "密码与确认密码不匹配。")] public stringConfirmPassword { get; 放; } (2认同)