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="&#39;Id&#39; must not be empty." data-val-number="The field Id must be a number." data-val="true">
验证有效,因为隐藏元素总是有一个值,但我对单选按钮有问题。例如,如果我不希望默认情况下始终选择一个单选按钮但为空,并且如果我想在该项目上放置验证规则,则渲染会将默认验证属性放在我的规则之上,因此它会变得混乱并且验证无法正常工作...
任何人都有类似的问题或知道这一点,或者我是否必须提取 ASP.NET MVC 源代码并自己查找? :)
半懒惰和按截止日期小推的编码器
编辑:
我尝试了此链接中提出的解决方案:
Fluent Validations。错误:不显眼的客户端验证规则中的验证类型名称必须是唯一的
,但 ASP.NET MVC 会在每个字段上发出必需的属性,无论 AddImplicitRequiredAttribute 设置如何...
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="'Id' 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将视图模型上的
Id
属性设置为可为 null 的整数。而不是
因此,您应该使用:
:对于您不希望需要的任何其他值类型,同样适用。您应该使用可空类型。
Html 帮助程序会自动为所有不可为 null 的类型发出
data-val
属性,这是设计使然,如果您不希望出现此行为,则必须编写自己的自定义 HTML 帮助程序来生成输入字段和下拉列表。您不能再依赖 TextBoxFor 和 DropDownListFor 帮助程序,因为它们就是这样设计的。Make the
Id
property on your view model a nullable integer.So instead of:
you should use:
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.