为什么 @html.editorfor和 @html.passwordfor在MVC中创建不同的样式框?

发布于 2025-02-11 03:53:14 字数 1670 浏览 2 评论 0原文

我目前正在使用@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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘦怹 2025-02-18 03:53:14

我不能告诉你为什么决定以不同的方式对待这些问题,而是这些特定过载的第二个参数editorfor()都接受匿名对象,这些对象实际上代表了不同的概念。

editorfor() ,第二个参数标题为fromeviewdata

可以包含其他视图数据的匿名对象?view = aspnet-mvc-5.2“ rel =“ nofollow noreferrer”> viewDatadictionary&lt; tmodel&gt; 为模板创建的实例。

For

一个包含为元素设置的HTML属性的对象。

换句话说

,您在这里在不同级别上运行。当您在匿名对象上设置htmlattributes editorfor()(即, afterviewdata )时,该属性在您的渲染元素:

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

但是,当您在匿名对象上设置htmlattributes passwordfor()(即,htmlattributes),被视为html正如您在HTML输出中看到的那样,属性本身:

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

分辨率

结果 ,您在提升htmlattributes password> password> password> passwordfor()的一个级别时,您应该做的呼叫:

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

应该呈现类似的内容:

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

正确设置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() and PasswordFor() both accept an anonymous object, those objects actually represent different concepts.

Documentation

For EditorFor(), the second parameter is titled additionalViewData:

An anonymous object that can contain additional view data that will be merged into the ViewDataDictionary<TModel> instance that is created for the template.

For PasswordFor(), the second parameter is titled htmlAttributes:

An object that contains the HTML attributes to set for the element.

Explanation

In other words, you're operating at different levels here. When you set a property called htmlAttributes on your anonymous object for EditorFor() (i.e., additionalViewData), that's being parsed out as HTML attributes on your rendered element:

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

But when you set htmlAttributes on your anonymous object for PasswordFor() (i.e., htmlAttributes), that's being seen as an HTML attribute itself, as you can see in your HTML output:

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

Resolution

As a result, what you should be doing in elevating the htmlAttributes one level for your PasswordFor() call:

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

Which should render something like:

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

And with the CSS classes correctly set, you should also find that the presentation aligned.

凉城 2025-02-18 03:53:14

方法签名略有不同。

public static System.Web.Mvc.MvcHtmlString PasswordFor<TModel,TProperty> (this System.Web.Mvc.HtmlHelper<TModel> htmlHelper,
     System.Linq.Expressions.Expression<Func<TModel,TProperty>> expression,
     object htmlAttributes);

public static System.Web.Mvc.MvcHtmlString EditorFor<TModel,TValue> (this System.Web.Mvc.HtmlHelper<TModel> html,
    System.Linq.Expressions.Expression<Func<TModel,TValue>> expression,
    object additionalViewData);

您无需将passwordfor属性对象包装在另一个对象中。您拥有的是:

@Html.EditorFor(m => m.Password, additionalViewData: new { htmlAttributes = new { ... } })
@Html.PasswordFor(m => m.Pwd, htmlAttributes: new { htmlAttributes = new { ... } })

简直就是:

@Html.PasswordFor(m => m.Pwd, new { @class = "..." })

The method signatures are slightly different.

public static System.Web.Mvc.MvcHtmlString PasswordFor<TModel,TProperty> (this System.Web.Mvc.HtmlHelper<TModel> htmlHelper,
     System.Linq.Expressions.Expression<Func<TModel,TProperty>> expression,
     object htmlAttributes);

public static System.Web.Mvc.MvcHtmlString EditorFor<TModel,TValue> (this System.Web.Mvc.HtmlHelper<TModel> html,
    System.Linq.Expressions.Expression<Func<TModel,TValue>> expression,
    object additionalViewData);

You don't need to wrap the PasswordFor attribute object in another object. What you have is:

@Html.EditorFor(m => m.Password, additionalViewData: new { htmlAttributes = new { ... } })
@Html.PasswordFor(m => m.Pwd, htmlAttributes: new { htmlAttributes = new { ... } })

It is simply:

@Html.PasswordFor(m => m.Pwd, new { @class = "..." })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文