@ Html.CheckBoxFor生成客户端验证属性,而@ Html.CheckBox不 [英] @Html.CheckBoxFor generates client-side validation attributes, while @Html.CheckBox doesn't

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

问题描述

让我们说我有这个模型:

 公共类Person
{
    公共BOOL IsApproved {搞定;组; }
}

和WHIS这个codeS,我试图使输入检查键入:

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

但是,结果是不同的:

  // CheckBoxFor结果
<输入数据-VAL =真正的数据-VAL-所需=是必需的IsApproved场。 ID =IsApprovedNAME =IsApproved类型=复选框VALUE =真>
<输入名称=IsApproved类型=隐藏值=假>//复选框结果
<输入ID =IsApprovedNAME =IsApproved类型=复选框VALUE =真>
<输入名称=IsApproved类型=隐藏值=假>

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

更新:

交换的 @ Html.CheckBoxFor @ Html.CheckBox 标记元素的顺序没订单后'T改变。


解决方案

复选框()助手不呈现数据-VAL 属性,因为形式已经呈现 CheckBoxFor()同一属性。如果交换顺序,数据-VAL 属性将呈现为复选框()(而不是 CheckBoxFor())。

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

控件的HTML辅助内部调用 GetUnobtrusiveValidationAttributes() 的HtmlHelper 的方法。 从源头code (我的重点)


  

仅在启用不显眼的客户端验证渲染属性,,然后只有当我们从来没有以这种形式呈现验证一个字段具有此名称。此外,如果没有语境的形式,那么我们就无法呈现的属性(我们就没有附上他们)


 公开的IDictionary<字符串对象> GetUnobtrusiveValidationAttributes(字符串名称,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.CheckBoxFor生成客户端验证属性,而@ Html.CheckBox不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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