Html PasswordFor helper in MVC3 Razor
- Html.PasswordFor is the helper which renders a Textbox.
- Helpers are not controls by itself, they simply generate html markup.
- It renders a textbox which is different from basic textbox.
- The textbox rendered using Password helper displays text entered inside it in password format i.e. bullets are displayed.
- Html.PasswordFor binds the control with model property.
It has following overloads :
Overload 1 :
This overload of PasswordFor helper accepts linq expression, which binds the control to the model property.
Syntax :
<p>
@Html.PasswordFor(c => c.Password)
</p>
UI :
Rendered HTML :
<p>
<input id="Password" name="Password" type="password" />
</p>
The linq expression binds the Password property of model to password control. The display name property of model property is used to set id and name attribute of the control.
Overload 2 :
This overload also accepts htmlAttribute object along with linq expression.
Syntax :
<p>
@Html.PasswordFor(c => c.Password, new { maxlength = "10"})
</p>
UI :
Rendered HTML :
<p>
<input id="Password" maxlength="10" name="Password" type="password" />
</p>
The maxlength attribute for password control is set using htmlAttribute object.
Overload 3 :
This overload accepts IDictionary htmlAttributes object along with linq expression.
Syntax :
<p>
@Html.PasswordFor(c => c.Password,dd)
</p>
In above example, we are passing IDictionary object of attributes as parameter. Using this overload we can pass the collection of attributes instead of specifying attributes one by one.
UI :
Rendered HTML :
<p>
<input id="Password" maxlength="20" name="Password" type="password" />
</p>
Is there an option for @Html.PasswordFor to display the text if a user wishes to check the password they input for spelling?
ReplyDelete