具有层次结构意识的 MVC3 自定义验证
我在 MVC3 中有一个分层模型。我所有的基本验证都有效,但我有一个特殊的验证,我不知道如何实现。
在我的特定树层次结构模型中,我有一个字段必须是唯一的/不重复其任何祖先节点中的值。就我而言,同行不必是独一无二的。
我尝试继承 ValidationAttribute
并使用 validationContext.Items
来存储父节点堆栈,但在我看来, validationContext.Items
在每个级别的验证之间共享。我不知道如何正确地递归调用每个孩子的验证(如果合适的话)。
如果我要验证
特定节点,我需要访问祖先节点,或者我为传递给每个子节点的祖先节点创建的一些堆栈。
该解决方案不应干扰其他验证器或绑定器,并且应提供字段级错误而不是根模型级错误。我还想避免 ThreadStatic、保留字符串和其他魔术。是的,使用 这种技术。
谢谢!
I have a hierarchical model in MVC3. All my basic validation is working, but I have a special validation that I can't figure out how to implement.
In my particular tree-hierarchy model, I have a field which must be unique/not repeat the values in any of its ancestral nodes. In my case, peers do not have to be unique.
I've tried to inherit ValidationAttribute
and use validationContext.Items
to store a stack of parent nodes, but it doesn't seem to me that the validationContext.Items
is shared between the validation of each level. I don't know how to recursively call the validation on each child correctly, if that is appropriate.
If I'm Validating
a particular node, I need access to the ancestral nodes, or some stack I create of the ancestral nodes passed to each child.
The solution shouldn't interfere with other validators or binders and should provide field-level errors rather than root-model level errors. I also want to avoid ThreadStatic,
reserved strings, and other magic tricks. Yes, the entire hierarchy is bound in a single view using this technique.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能创建一个动作过滤器吗?这使您可以访问 modelstate 和 valueProvider,以便您可以检查数据,然后根据需要调整 Errors 集合。
Could you create an action filter? This give you access to the modelstate and valueProvider so you could check the data and then adjust the Errors collection as necessary.