Asp.net

創建密碼欄位@html.TextBoxFor

  • June 5, 2020

模型類

[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 但我必須為 textbox 應用 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" })

引用自:https://stackoverflow.com/questions/29329997