質問編集履歴

1

環境の追記、特定処理部分のコード追記、検証部分の追記、を行いました。

2023/03/02 00:05

投稿

TomatoSaba
TomatoSaba

スコア12

test CHANGED
File without changes
test CHANGED
@@ -1,9 +1,19 @@
1
+ ◇環境
2
+ OS:Windows10
3
+ IDE:VisualStudio2015
4
+ framework:.NET Framework 4.6.1
5
+
6
+
1
7
  ◇実現したい事
2
- 以下の様なModelについて
8
+ 以下のModelの様検証を実装している場合について
9
+ 下記、検索処理(Kensaku)のコード中の
10
+ 「リストデータの検証は不要」の箇所においては
3
- Rowsの検証エラーをとある処理の際にはクリアしたいと考えております。
11
+ Rows要素入力検証を無効としたいと考えております。
12
+
4
13
 
5
14
  ```Model
6
15
  Public Class Page1
16
+ <CustomValidation(GetType(Page1), "SearchRequired")>
7
17
  Public Property InDate As Date?
8
18
 
9
19
  Public Property Rows As New List(Of Page1.Row)
@@ -11,10 +21,49 @@
11
21
  Public Property RowData As Row
12
22
 
13
23
  Public Class Row
24
+ <RegularExpression("NG", ErrorMessage:="NGはNG")>
14
25
  Public Property ITEM1 As String
26
+
27
+ <RegularExpression("NG", ErrorMessage:="NGはNG")>
15
28
  Public Property ITEM2 As String
29
+
16
30
  End Class
31
+
32
+ Public Shared Function SearchRequired(m As String, context As ValidationContext) As ValidationResult
33
+ Dim kensaku = CType(context.ObjectInstance, Page1)
34
+
35
+ If Not kensaku.InDate Then
36
+ Return New ValidationResult(String.Format(”{0}は必須です”, "日付"))
37
+ End If
38
+
39
+ Return ValidationResult.Success
40
+
41
+ End Function
42
+
17
43
  End Class
44
+ ```
45
+
46
+ ```Kensaku
47
+ <HttpPost()>
48
+ <ValidateAntiForgeryToken>
49
+ Function Index(model As Page1) As ActionResult
50
+
51
+ 'リストデータの検証は不要(以下が動作しない)
52
+ 'ModelState("Rows").Errors.Clear()
53
+
54
+ ' 入力チェック
55
+ If Not ModelState().IsValid Then
56
+ Return View("Index", model)
57
+ End If
58
+
59
+ ModelState.Clear()
60
+
61
+ ' 検索処理を呼び出し
62
+ Search(model)
63
+
64
+ Return RedirectToAction("Index", "Page1")
65
+
66
+ End Function
18
67
  ```
19
68
 
20
69
  ◇試したこと