Asp.net 创建密码字段@html.TextBoxFor

Asp.net 创建密码字段@html.TextBoxFor,asp.net,asp.net-mvc,razor,Asp.net,Asp.net Mvc,Razor,模范班 [Required(ErrorMessage = "Pass word is required")] //[ValidatePasswordLength] [DataType(DataType.Password)] //[Display(Name = "Password")] public string Password { get; set; } 看法 @LabelFor(model=>model.Password) @Html.TextBoxFor(model=>model.Pa

模范班

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


@LabelFor(model=>model.Password)
@Html.TextBoxFor(model=>model.Password,新的{@class=“form control”,placeholder=“Password”})
@Html.ValidationMessageFor(model=>model.Password)
我在谷歌上搜索了一下。我发现使用
@html.Editor
而不是@html.TextBoxFor,但我必须为textbox应用css

我使用了
@Html.TextBoxFor(model=>model.Password,新的{@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.textbox用于:

@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中的Password属性中添加数据注释

[DataType(DataType.Password)]
public String Password { get; set; }
在查看页面中,只需键入以下代码

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

您可以尝试使用html
输入类型

@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"})
@Html.Password("Passwordvalue", null, new {@class="form-control", @id= "Passwordvalue", 
placeholder = "Password"})