依赖数据注释MVC [英] Dependent DataAnnotations MVC

查看:69
本文介绍了依赖数据注释MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在接受采访时被问到一个关于MVC数据注释验证的问题。



我有一个下拉列表和两个文本框。

如果从下拉列表中选择了某个值(比如说XYZ),那么对文本框的应用验证是必需的,文本框在提交时可以留空。



注意:这不应该通过jQuery和自定义注释来处理,只能通过数据注释来处理。



我无能为力。这个问题是否有效?



感谢任何帮助。

I was asked a question in an interview regarding validation with data annotations in MVC.

I have a drop down list and two text boxes.
If some value is selected from the drop down (say "XYZ"), then the apply validation on textboxes to be required else, textboxes can be left blank on submit.

Note: This shouldn't be handled via jQuery and custom annotations, only through data annotations.

I was left clueless. Is this question a valid one?

Appreciate any help.

推荐答案

几个选项:



1)使模型类继承自IValidatableObject并实现validate方法,参见https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.ivalidatableobject.aspx [ ^ ]





虽然没有使用数据注释,但是......



2)https://github.com/JaroslawWaliszko/ExpressiveAnnotations [ ^ ]允许对于可以与模型中的其他值进行比较的RequiredIF注释。
Couple of options:

1) Make your model class inherit from IValidatableObject and implement the validate method, see https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.ivalidatableobject.aspx[^]


That though is not using the Data Annotations, but...

2) https://github.com/JaroslawWaliszko/ExpressiveAnnotations[^] allows for a RequiredIF annotation that can compare against other values in the model.


这篇关于依赖数据注释MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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