如何在字段上重新启用ASP.NET MVC非侵入式验证 [英] How to re-enable ASP.NET MVC unobtrusive validation on a field

查看:69
本文介绍了如何在字段上重新启用ASP.NET MVC非侵入式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,设置了不干扰人的验证,并且在大多数情况下都可以正常工作.但是,在某些情况下,我必须禁用表单中的select元素,然后有时重新启用该元素(通常在QA尝试所有可能的情况时,但仍然如此).发生这种情况时,select元素上的必需"验证规则将不再起作用.

I have a form with unobtrusive validation set up and all works well under most circumstances. However there are cases where I have to disable a select element in the form and then sometimes the element gets re-enabled (Mostly when QA is trying every possible circumstance but still). When this happens the "required" validation rule on the select element no longer works.

我尝试从元素的规则集合中删除并重新添加所需的规则,但无济于事.我尝试接线并调用描述为此处,这也不起作用.

I have tried removing and re-adding the required rule from the element's rules collection to no avail. I have tried wiring in and calling the validation extension described here which also does not work.

尽管我很想知道问题的根本原因,但现在我也很乐意接受黑客和变通办法.我觉得这是一个已解决的问题,但我在搜索中没有看到它.

While I would love to know the root cause of the issue I am also open at this point to hacks and workarounds. I have a feeling this is a solved problem but I am not seeing it in my searches.

谢谢, 马修

更新:我很想选择一个答案,而不是愚蠢地犯错,但这并不能使网络更好.发生的情况是select元素中的选项基于另一个字段是动态的.当基于该其他字段的选项中没有要输入的项目并且重新启用选择时,将自动填充该选项.当我重新创建满足必需"规则的选项列表时,我正在做的是在默认选项中添加"0"值.我将默认值更改为空字符串,问题消失了.

UPDATE: I am tempted to just select an answer and not fess up to my silly mistake, but that doesn't make the web better. What was going on is that the options in the select element are dynamic based on another field. The disabling happens when there are not items to go in the options based on that other field and when re-enabling the select the options are populated freshly. What I was doing was putting a "0" value in the default option when I recreated the options list which satisfied the "required" rule. I changed the default value to empty string and the issue went away.

处理似乎不是问题的协议是什么?我应该关闭它吗?

What is the protocol for dealing with a question that isn't what it appeared to be? Should I close it?

推荐答案

为了重新解析非侵入式验证属性,必须首先清除在首次解析属性时创建的验证器和unobtrusiveValidation数据.然后,您可以像这样重新解析:

In order to re-parse unobtrusive validation attributes, you must first clear validator and unobtrusiveValidation data created when the attributes were first parsed. Then, you can re-parse, like so:

$("form").removeData("validator");  
$("form").removeData("unobtrusiveValidation");  
$.validator.unobtrusive.parse("form");  

这篇关于如何在字段上重新启用ASP.NET MVC非侵入式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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