How do I convert a textbox type to password in asp.net mvc?
There are many types of using password-typed textboxes in ASP. NET MVC
With html controls it would be like;
<input type="password" name="xx" id="yy">
With Razor-syntax it would be like;
@Html.Password(name, value, html_attributes)
Or in a strongly typed view you could write
@Html.PasswordFor(model=>model.Password)
Another way is to add @type = password
to your html.texbox()
attributes like so:
before converting:
@Html.TextBox("mypass", null, new { @class = "" })
After converting:
@Html.TextBox("mypass", null, new { @class = "", @type = password })
This will mask your text box text with circles instead of text. It does this, because the @type = password
attribute tells your text box that it is of type "password".
This method of converting your text box to type password is an easy quick way to to make the conversion from your standard razor form text box.
In .cshtml file:
@Html.PasswordFor(modal => modal.Password)
Just write in Razor View i.e. in .cshtml file below line
@Html.PasswordFor(m=>m.Password)
Here m is model object and password is the password field name.
Either use the HTML helper function Password
:
<%= Html.Password("Password") %>
or use the type
parameter on an input field:
<input name="password" type="password" />
See listings 4 and 5 on this page
below is extention based on html helper:
before converting:
@Html.TextBox("mypass", null, new { style = "width: 200px;" })
After converting:
@Html.Password("mypass", null, new { style = "width:200px;" })
hope helps someone.