模型类
[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>
我用Google搜索了。我发现使用@html.Editor
而不是@html.TextBoxFor,但我必须为文本框应用css。
我使用@Html.TextBoxFor(model => model.Password, new {@class="form-control",placeholder="Password" })
但css未应用。
我如何实现这一目标?
答案 0 :(得分:27)
如果您的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"})
答案 1 :(得分:13)
您仍然可以使用@ Html.TextBoxFor:
@Html.TextBoxFor(model => model.Password, new {@class="form-control", placeholder="Password", @type = "password" })
答案 2 :(得分:8)
我也遇到了同样的问题并且已经使用了下面的代码,但我不知道为什么它不起作用
@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"}})
答案 3 :(得分:1)
您可以尝试使用html input type
@Html.TextBoxFor(model => model.Password, new {@class="form-control", placeholder="Password", @type = "password" })
答案 4 :(得分:0)
这也有效-
@Html.Password("Passwordvalue", null, new {@class="form-control", @id= "Passwordvalue",
placeholder = "Password"})