我目前正在使用@Html.EditorFor HTML helper作为密码字段.我想改用@Html.PasswordFor HTML助手.

我复制了当前代码,并将@Html.EditorFor替换为@Html.PasswordFor.

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" } })

渲染输出

这些参数完全相同,但会生成不同样式的文本框:

enter image description here

注意,@Html.EditorFor有验证和占位符,但@Html.PasswordFor没有;后者也有不同的风格.validation spam元素也不是textbox的一部分.

生成的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; }

我做错了什么?

推荐答案

我不能告诉您why决定以不同的方式对待这些对象,但尽管这些特定重载的第二个参数EditorFor()PasswordFor()都接受匿名对象,但这些对象实际上代表不同的概念.

文档

对于EditorFor(),第二个参数标题为additionalViewData:

可以包含其他视图数据的匿名对象,这些数据将合并到为模板创建的ViewDataDictionary<TModel>实例中.

对于PasswordFor(),第二个参数标题为htmlAttributes:

包含要为元素设置的HTML属性的对象.

解释

换言之,您在这里的操作处于不同的级别.当您在匿名对象上为EditorFor()(即additionalViewData)设置一个名为htmlAttributes的属性时,该属性将被解析为渲染元素上的HTML属性:

<input autocomplete="off" class="…" id="password" placeholder="password" … > 

但是,当您在匿名对象上为PasswordFor()设置htmlAttributes(即htmlAttributes)时,这被视为HTML属性本身,正如您在HTML输出中所看到的:

<input htmlattributes="{ class = …, placeholder = password, id = password, autocomplete = off }" … >

决议

因此,您在PasswordFor()通话中将htmlAttributes提高一个级别时所做的是:

@Html.PasswordFor(x => x.Pwd, new { @class = "form-control k-textbox large", placeholder = "password", @id = "password", @autocomplete = "off" })

其中should个渲染如下:

<input class="form-control k-textbox large" placeholder="password" id="password" autocomplete="off" … >

正确设置CSS类后,您还应该发现演示文稿是一致的.

Asp.net相关问答推荐

如何使用 Get-Process powershell 命令通过端口号获取进程的 ID

目前不支持超过 65534 个段的跨区存档

如何在没有 Select 按钮的情况下在 GridView 中实现全行 Select ?

如何在 ASP.NET 中以 'YYYY-MM-DD' 格式获取当前日期?

静态字段与会话变量

无法获取项目引用的依赖项

获取在 asp.net identity 2.0 中分配了角色的用户列表

如何将 Web 应用程序项目转换为类库项目

asp.net 中的 Eval() 有什么用

如何在 ASP.NET MVC3 中配置区域

防止在 Visual Studio 2008 ASP.NET Web 窗体中复制粘贴时自动生成 ID

为应用程序池Classic .NET AppPool提供服务的进程与 Windows 进程激活服务发生了致命的通信错误

无法序列化会话状态

缩小 ASP.NET 应用程序的 Html 输出

如何获取正在访问 ASP.NET 应用程序的当前用户?

Web API 必填参数

ASP.NET 5、EF 7 和 SQLite - SQLite 错误 1:没有这样的表:博客

ASP.NET 按钮重定向到另一个页面

根据条件更改 GridView 行 colored颜色

~/ 等价于 javascript