第一个 html helper 生成客户端验证属性,而第二个不生成 [英] First html helper generates client-side validation attributes, while the second one doesn't

查看:13
本文介绍了第一个 html helper 生成客户端验证属性,而第二个不生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个模型:

公共类人{public bool IsApproved { get;放;}}

这是代码,我正在尝试使用 check 类型呈现 input:

@Html.CheckBoxFor(x => x.IsApproved)@Html.CheckBox("IsApproved")

但是,结果是不同的:

//CheckBoxFor 结果<input data-val="true" data-val-required="IsApproved 字段是必需的."id="IsApproved" name="IsApproved" type="checkbox" value="true"><input name="IsApproved" type="hidden" value="false">//复选框结果<input id="IsApproved" name="IsApproved" type="checkbox" value="true"><input name="IsApproved" type="hidden" value="false">

如何以及为什么,第一个生成用于客户端验证的属性,而另一个没有?

更新:

交换@Html.CheckBoxFor@Html.CheckBox的顺序后,标记元素的顺序没有改变.

解决方案

CheckBox() 帮助器不渲染data-val 属性,因为表单已经渲染CheckBoxFor() 用于相同的属性.如果您交换顺序,data-val 属性将为 CheckBox() 呈现(而不是为 CheckBoxFor())呈现.>

我的理解是,在解析表单时,这会导致 jquery.validation.unobtrusive 出现潜在的(重复)问题.

控件的 html 助手在内部调用 HtmlHelperGetUnobtrusiveValidationAttributes() 方法.来自源代码(我的重点)

<块引用>

仅当启用了不显眼的客户端验证时才呈现属性,然后仅当我们从未在此表单中为具有此名称的字段呈现验证.此外,如果没有表单上下文,则我们无法呈现属性(我们不必将它们附加到)

public IDictionaryGetUnobtrusiveValidationAttributes(字符串名称,ModelMetadata 元数据){

Let's say I have this model:

public class Person
{
    public bool IsApproved { get; set; }
}

And whis this codes, I am trying to render input with check type:

@Html.CheckBoxFor(x => x.IsApproved)
@Html.CheckBox("IsApproved")

But, the results are different:

// CheckBoxFor result
<input data-val="true" data-val-required="The IsApproved field is required." id="IsApproved" name="IsApproved" type="checkbox" value="true">
<input name="IsApproved" type="hidden" value="false">

// CheckBox result
<input id="IsApproved" name="IsApproved" type="checkbox" value="true">
<input name="IsApproved" type="hidden" value="false">

How and why, the first one generates attributes for client-side validation, while the other didn't?

Update:

After swapping the order of @Html.CheckBoxFor and @Html.CheckBox, the order of markup elements didn't change.

解决方案

The CheckBox() helper does not render thedata-val attributes because the form has already rendered CheckBoxFor() for the same property. If you swap the order, the data-val attributes would be rendered for CheckBox() (and not for CheckBoxFor()).

My understanding is this would cause a potential (duplication) problem with jquery.validation.unobtrusive when parsing the form.

The html helpers for controls internally call the GetUnobtrusiveValidationAttributes() method of HtmlHelper. From the source code (my emphasis)

Only render attributes if unobtrusive client-side validation is enabled, and then only if we've never rendered validation for a field with this name in this form. Also, if there's no form context, then we can't render the attributes (we'd have no to attach them to)

public IDictionary<string, object> GetUnobtrusiveValidationAttributes(string name, ModelMetadata metadata)
{

这篇关于第一个 html helper 生成客户端验证属性,而第二个不生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆