King_Fisher King_Fisher - 1 month ago 18
ASP.NET (C#) Question

Create Password field @html.TextBoxFor

Model class

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


view

<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>


I googled it.I found to use
@html.Editor
instead of @html.TextBoxFor but i have to apply css for textbox .

I used
@Html.TextBoxFor(model => model.Password, new {@class="form-control",placeholder="Password" })
but css not applied.
How do i achieve this?

Answer

If your MVC version is new enough, then

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

Otherwise

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