默认情况下,asp.net mvc 3中隐藏,无线电,下拉元素上的客户端验证属性 [英] Clientside validation attributes on hidden,radio, dropdown elements by default in asp.net mvc 3

查看:98
本文介绍了默认情况下,asp.net mvc 3中隐藏,无线电,下拉元素上的客户端验证属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC 3 + FLUENT VALIDATION + NINJECT

我有模型X,该模型的规则如下:

RuleFor(c => c.FirstName).NotEmpty();
RuleFor(c => c.LastName).NotEmpty();

我发现了一个奇怪的事情,引擎在所有隐藏的元素以及下拉菜单,单选按钮等上放置了验证属性,并且我没有在该模型的验证配置中指定此属性,所以我猜是默认情况下...

<input type="hidden" value="1" name="Id" id="Id" data-val-required="&amp;#39;Id&amp;#39; must not be empty." data-val-number="The field Id must be a number." data-val="true">

验证之所以有效,是因为隐藏的元素始终具有一个值,但是单选按钮存在问题.例如,如果我不希望默认情况下总是选择一个单选按钮,而是将其设为空,并且如果我想在该项目上放置验证规则,则渲染会将默认验证属性和规则放在我的规则之上,因此它变得一团糟并且验证无法正常工作...

任何人都有类似的问题或对此有所了解,还是我必须拉ASP.NET MVC源并自己查找它? :)

半延迟和按时按一下编码器

修改:

我尝试通过此链接提出建议的解决方案:

流利验证.错误:不打扰的客户端验证规则中的验证类型名称必须唯一

但是,无论AddImplicitRequiredAttribute设置如何,asp.net mvc都会在每个字段上发出必需的属性...

解决方案

使视图模型的Id属性为可为空的整数.

所以代替:

public int Id { get; set; }

您应该使用:

public int? Id { get; set; }

对于不需要的其他任何值类型,也是如此.您应该使用可为空的类型.

HTML帮手会自动为所有非空类型发出data-val属性,这是设计使然,如果您不希望出现这种情况,则必须编写自己的自定义HTML帮手来生成输入字段和下拉列表.您不能再依赖TextBoxFor和DropDownListFor助手了,因为它们是这样设计的.

I'm using ASP.NET MVC 3 + FLUENT VALIDATION + NINJECT

I have model X and rules for that model are something like this:

RuleFor(c => c.FirstName).NotEmpty();
RuleFor(c => c.LastName).NotEmpty();

I spotted a little strange thing, the engine puts validation attributes on all hidden elements and on dropdowns, radio buttons, etc.., and I didn't specified this in the validation configuration for that model, so I guess it is by default...

<input type="hidden" value="1" name="Id" id="Id" data-val-required="&amp;#39;Id&amp;#39; must not be empty." data-val-number="The field Id must be a number." data-val="true">

Validation works because hidden element always have a value, but I have a problem with radio buttons. For example, if I don't want one radio button always to be selected by default but empty and if I want to put validation rules on that item, the rendering puts default validation attributes and on top of my rules, so it's getting messed up and validation doesn't work properly...

Anyone had similar issues or knows about this, or do I have to pull the ASP.NET MVC source and look it up by myself? :)

Semi-Lazy and little-pushed-down-by-deadlines coder

Edit:

I tried proposed solution from this link:

Fluent Validations. Error: Validation type names in unobtrusive client validation rules must be unique

but asp.net mvc emits required attributes on each field regardless of AddImplicitRequiredAttribute settings...

解决方案

Make the Id property on your view model a nullable integer.

So instead of:

public int Id { get; set; }

you should use:

public int? Id { get; set; }

Same stands true for any other value types that you don't want to be required. You should use nullable types.

Html helpers automatically emit data-val attributes for all non-nullable types which is by design and if you do not want this behavior you will have to write your own custom HTML helpers to generate your input fields and dropdowns. You can no longer rely on TextBoxFor and DropDownListFor helpers as that's how they are designed.

这篇关于默认情况下,asp.net mvc 3中隐藏,无线电,下拉元素上的客户端验证属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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