为什么 @html.editorfor和 @html.passwordfor在MVC中创建不同的样式框?
我目前正在使用@html.editorfor
html助手用于密码字段。我想使用@html.passwordfor
html助手。
我复制了当前代码,并用@html.passwordfor
替换了@html.editorfor
。
cshtml
这是代码:
@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" } })
渲染输出
参数是100%相同的,但是它们会产生不同样式的文本框:
”好吧,但是@html.passwordfor
没有;后来的风格也不同。验证垃圾邮件
元素也不是文本框的一部分。
生成的html
以下是生成@html.editorfor
的HTML代码:
<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">
这是生成@html.passwordfor
的html代码:
<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">
模型定义
这是我如何定义这两个字段的方式模型:
[Required(ErrorMessage = " ")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required(ErrorMessage = " ")]
[DataType(DataType.Password)]
public string Pwd { get; set; }
我在这里做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不能告诉你为什么决定以不同的方式对待这些问题,而是这些特定过载的第二个参数
editorfor()
都接受匿名对象,这些对象实际上代表了不同的概念。?
editorfor()
,第二个参数标题为fromeviewdata
:For
换句话说
,您在这里在不同级别上运行。当您在匿名对象上设置
htmlattributes
editorfor()
(即, afterviewdata )时,该属性在您的渲染元素:但是,当您在匿名对象上设置
htmlattributes
passwordfor()
(即,htmlattributes
),被视为html正如您在HTML输出中看到的那样,属性本身:分辨率
结果 ,您在提升
htmlattributes
password> password> password> passwordfor()的一个级别时,您应该做的
呼叫:应该呈现类似的内容:
正确设置CSS类,您还应该发现演示文稿对齐。
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
htmlAttributes
on your anonymous object forEditorFor()
(i.e.,additionalViewData
), that's being parsed out as HTML attributes on your rendered element:But when you set
htmlAttributes
on 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
htmlAttributes
one level for yourPasswordFor()
call:Which should render something like:
And with the CSS classes correctly set, you should also find that the presentation aligned.
方法签名略有不同。
您无需将
passwordfor
属性对象包装在另一个对象中。您拥有的是:简直就是:
The method signatures are slightly different.
You don't need to wrap the
PasswordFor
attribute object in another object. What you have is:It is simply: