I am currently using the @Html.EditorFor HTML helper for the password field. I want to use the @Html.PasswordFor HTML helper instead.
I copied the current code and replaced @Html.EditorFor with @Html.PasswordFor.
CSHTML
This is the code:
@Html.EditorFor(x => x.Password, new { htmlAttributes = new { @class = "form-control k-textbox large", placeholder = "password", @id = "password", @autocomplete = "off" } })
@Html.PasswordFor(x => x.Pwd, new { htmlAttributes = new { @class = "form-control k-textbox large", placeholder = "password", @id = "password", @autocomplete = "off" } })
Rendered Output
The parameters are 100% the same, but these produce different style textboxes:
Note that @Html.EditorFor has validation and a placeholder as well, but @Html.PasswordFor doesn't; the later also has a different style. The validation spam element is also not a part of the textbox.
Generated HTML
Here is generated HTML code for @Html.EditorFor:
<input autocomplete="off" class="form-control k-textbox large text-box single-line password k-invalid" data-val="true" data-val-required=" " id="password" name="Password" placeholder="password" type="password" value="" aria-invalid="true">
Here is generated HTML code for @Html.PasswordFor:
<input data-val="true" data-val-required=" " htmlattributes="{ class = form-control k-textbox large, placeholder = password, id = password, autocomplete = off }" id="Pwd" name="Pwd" type="password" aria-invalid="true" class="k-invalid">
Model Definition
This is how I define those two fields in the model:
[Required(ErrorMessage = " ")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required(ErrorMessage = " ")]
[DataType(DataType.Password)]
public string Pwd { get; set; }
What am I doing wrong here?

I can't tell you why the decision was made to treat these differently, but while the second parameter for these particular overloads of
EditorFor()andPasswordFor()both accept an anonymous object, those objects actually represent different concepts.Documentation
For
EditorFor(), the second parameter is titledadditionalViewData:For
PasswordFor(), the second parameter is titledhtmlAttributes:Explanation
In other words, you're operating at different levels here. When you set a property called
htmlAttributeson your anonymous object forEditorFor()(i.e.,additionalViewData), that's being parsed out as HTML attributes on your rendered element:But when you set
htmlAttributeson your anonymous object forPasswordFor()(i.e.,htmlAttributes), that's being seen as an HTML attribute itself, as you can see in your HTML output:Resolution
As a result, what you should be doing in elevating the
htmlAttributesone level for yourPasswordFor()call:Which should render something like:
And with the CSS classes correctly set, you should also find that the presentation aligned.