1

クラス プロパティに基本的な必須長さと最小長で注釈を付け、asp.net mvc サーバー側とクライアント側の検証のすべての利点を得ることを理解しています。

ただし、この「基本」検証をより複雑なビジネスルールと組み合わせる方法を示すリンクはありますか? たとえば、顧客が昨年何かを注文した場合 (データベース ヒットが必要)、同じ DataAnnotation と mvc 検証配管を引き続き使用するなど、ビジネス ルール機能を実行するにはどうすればよいでしょうか?

目標 : 検証メソッドを生成して出力する 2 つの方法を必要としません。

4

4 に答える 4

4

From http://msdn.microsoft.com/en-us/library/dd901590%28VS.95%29.aspx:

To create customized validation checks, you can either create a class that derives from the ValidationAttribute class or create a method that performs the validation check and reference that method when applying the CustomValidationAttribute to the data member. When you create a class that derives from ValidationAttribute, override the IsValid method to provide the logic for your customized validation check.

There appears to be example code there.

于 2009-10-08T07:44:47.910 に答える
3

Data Annotation run before your action is invoked. Then, regardless whether the validation succeded or not, the action is still called. If the DA detected invalid data, your ModelState will be invalid.

Once here, you can still do any validation you want, for your business rules, as you would normally do without the data annotation, if you want to. In your action, you can add errors to the ModelState even if the Data Annotation validation passed. In this case, you add your errors with ModelState.addError, and those errors are added to any error provided by the DA. So in your View it doesn't matter where the error comes from.

Or, if your rules are general, you can write your own annotation tags. The Data Annotation thing is distributed with its source, so you have full control on it.

于 2009-10-08T07:38:36.637 に答える
1

次の記事をご覧ください。DataAnnotations を同じフィールドで複数回使用できます。N 個のプロパティと N 個の値を比較してください。

http://www.codeproject.com/KB/validation/MultipleDataAnnotations.aspx

于 2011-08-01T14:10:20.933 に答える