创建密码字段@html.TextBoxFor

Create Password field @html.TextBoxFor

型号class

[Required(ErrorMessage = "Pass word is required")]
//[ValidatePasswordLength]
[DataType(DataType.Password)]
//[Display(Name = "Password")]
public string Password { get; set; }

查看

<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Password, new {@class="form-control",placeholder="Password" })
@Html.ValidationMessageFor(model => model.Password)

</div>

我用谷歌搜索 it.I 发现使用 @html.Editor 而不是 @html.TextBoxFor 但我必须为文本框应用 css 。

我使用了 @Html.TextBoxFor(model => model.Password, new {@class="form-control",placeholder="Password" }) 但 css 没有应用。 我该如何实现?

如果你的MVC版本足够新,那么

@Html.EditorFor(model => model.Password, new { htmlAttributes = new {@class="form-control", placeholder="Password"}})

否则

@Html.PasswordFor(model => model.Password, new {@class="form-control", placeholder="Password"})

您仍然可以使用@Html.TextBoxFor:

@Html.TextBoxFor(model => model.Password, new {@class="form-control", placeholder="Password", @type = "password" })

我也遇到了同样的问题,已经使用了下面的代码,但我不知道为什么它不起作用

@Html.EditorFor(model => model.Password, new { htmlAttributes = new {@class="form-control", placeholder="Password"}})

但后来我找到了将 type ="password" 添加到上面

代码中的解决方案

那么,现在的代码是

@Html.EditorFor(model => model.Password, new { htmlAttributes = new {@class="form-control", placeholder="Password", Type="password"}})

更新 我找到了另一种解决方法,就是在 LoginViewModel

中向密码 属性 添加数据注释
[DataType(DataType.Password)]
public String Password { get; set; }

然后在查看页面中键入此代码

    @Html.EditorFor(model => model.Password, new { htmlAttributes = new {@class="form-control", placeholder="Password"}})

您可以尝试使用 html input type

@Html.TextBoxFor(model => model.Password, new {@class="form-control", placeholder="Password", @type = "password" })

这也有效-

@Html.Password("Passwordvalue", null, new {@class="form-control", @id= "Passwordvalue", 
placeholder = "Password"})