Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 449
King_Fisher Avatar asked Mar 29 '15 13:03

King_Fisher


People also ask

What is the difference between HTML textbox vs HTML TextBoxFor?

IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.

What is difference between Editorfor and TextBoxFor in MVC?

it's absolutly wrong answer, becuase the key difference is that Texbox returns input and editorfor returns your template where input is default template for editorfor.

How do I set TextBoxFor to ReadOnly?

The TextBoxes can be made ReadOnly by setting the HTML ReadOnly attribute using the HtmlAttributes parameter in Html. TextBox and Html. TextBoxFor helper functions. Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.

What is TextBoxFor?

TextBoxFor represents a single-line input control that allows end-users to enter text.


2 Answers

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"})
like image 183
GSerg Avatar answered Oct 14 '22 08:10

GSerg


You can still use @Html.TextBoxFor:

@Html.TextBoxFor(model => model.Password, new {@class="form-control", placeholder="Password", @type = "password" })
like image 18
Tekdream Avatar answered Oct 14 '22 07:10

Tekdream