King_Fisher
King_Fisher

Reputation: 1203

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?

Upvotes: 16

Views: 68549

Answers (5)

Shruti Sharma
Shruti Sharma

Reputation: 1

This is also working-

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

Upvotes: 0

Tejinder Singh
Tejinder Singh

Reputation: 1120

You can try using html input type

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

Upvotes: 1

Xiao
Xiao

Reputation: 199

I also got the same problem and already used the code below but I have no idea why it didn't work

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

but then I found the solution that is to add type ="password" into code above

then, the code is now

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

update I've found another way to fix that is that is just add data annotation to Password property in LoginViewModel

[DataType(DataType.Password)]
public String Password { get; set; }

and in the view page just type this code

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

Upvotes: 14

Tekdream
Tekdream

Reputation: 209

You can still use @Html.TextBoxFor:

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

Upvotes: 18

GSerg
GSerg

Reputation: 78134

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"})

Upvotes: 42

Related Questions