回答編集履歴
2
追伸
test
CHANGED
@@ -25,3 +25,81 @@
|
|
25
25
|
|
26
26
|
|
27
27
|
今の情報では、CustomValidator 自体に問題があるのか、それ以外なのかもわかりません。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
**【2018/3/23 追伸】**
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
念のため、上に紹介した記事「コレクションのデータアノテーション検証」のコードの Country クラスで、Name プロパティに CustomValidation を付与して検証してみましたが、期待通りエラーメッセージは表示されます。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
なので、質問者さんのやり方にどこか問題があるように思われます。
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
検証に使った Country クラスのコードは以下の通りです。Country クラス以外は記事のコードの通りで、一切変更していません。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
public class Country
|
50
|
+
|
51
|
+
{
|
52
|
+
|
53
|
+
public Country()
|
54
|
+
|
55
|
+
{
|
56
|
+
|
57
|
+
Details = new CountryInfo();
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
// 2018/3/23 CustomValidation は teratail の検証用
|
64
|
+
|
65
|
+
// https://teratail.com/questions/118421
|
66
|
+
|
67
|
+
[CustomValidation(typeof(Country), "ValidateCountryName")]
|
68
|
+
|
69
|
+
//[Required(ErrorMessage = "{0} は必須")]
|
70
|
+
|
71
|
+
[StringLength(15, ErrorMessage = "{0} は {1} 文字以内")]
|
72
|
+
|
73
|
+
[Display(Name = "国名")]
|
74
|
+
|
75
|
+
public String Name { get; set; }
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
public CountryInfo Details { get; set; }
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
public static ValidationResult ValidateCountryName(string name)
|
84
|
+
|
85
|
+
{
|
86
|
+
|
87
|
+
if (string.IsNullOrEmpty(name))
|
88
|
+
|
89
|
+
{
|
90
|
+
|
91
|
+
return new ValidationResult("null または Empty");
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
return ValidationResult.Success;
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
どこが違うのかチェックしてみてください。
|
1
誤記訂正
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
コレクションのモデルバインディングが
|
5
|
+
コレクションのモデルバインディングがうまく行われるようにするには、レンダリングされる html 要素の name 属性が連番のインデックスを含むようにする必要があるはずです。
|
6
6
|
|
7
7
|
|
8
8
|
|