回答編集履歴
3
追記
test
CHANGED
@@ -265,3 +265,13 @@
|
|
265
265
|
}
|
266
266
|
|
267
267
|
```
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
2017/6/25 13:03 追記がやりやいことと違う場合はその旨連絡ください。
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
その際は、先にお願いしたように、やりたいことや質問の背景&全体のシナリオ・ストーリーをきちんと書いてください。
|
2
一部訂正
test
CHANGED
@@ -28,7 +28,7 @@
|
|
28
28
|
|
29
29
|
|
30
30
|
|
31
|
-
上の紹介した記事は例としては好ましくなかったです。すみませんが忘れてください。代わりに以下に
|
31
|
+
上の紹介した記事は例としては好ましくなかったです。すみませんが忘れてください。代わりに以下に別の例を書いておきます。
|
32
32
|
|
33
33
|
|
34
34
|
|
1
追記追加
test
CHANGED
@@ -21,3 +21,247 @@
|
|
21
21
|
|
22
22
|
|
23
23
|
その際は、先にお願いしたように、やりたいことや質問の背景&全体のシナリオ・ストーリーをきちんと書いてください。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
----- 2017/6/25 13:03 追記 -----
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
上の紹介した記事は例としては好ましくなかったです。すみませんが忘れてください。代わりに以下にサンプルを書いておきます。
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
html の input type="text" 要素を使っても name 属性の設定が適切ならモデルバインディングは問題ありませんが、以下の問題があるので Html ヘルパーを使うことをお勧めします。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
(1) データアノテーション検証でクライアント側での検証ができない(サーバー側は検証されますが)。
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
(2) クライアント側での検証が不要ということであっても、サーバー側で検証 NG の場合、下の Controller のコードのように return View(z) としたいはずですが、それができない。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
**Model**
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
using System.Collections.Generic;
|
56
|
+
|
57
|
+
using System.Linq;
|
58
|
+
|
59
|
+
using System.Web;
|
60
|
+
|
61
|
+
using System.ComponentModel.DataAnnotations;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
namespace Mvc4App.Models
|
66
|
+
|
67
|
+
{
|
68
|
+
|
69
|
+
public class A
|
70
|
+
|
71
|
+
{
|
72
|
+
|
73
|
+
[Required(ErrorMessage="param1 は必須")]
|
74
|
+
|
75
|
+
public string param1 { set; get; }
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
public class B
|
82
|
+
|
83
|
+
{
|
84
|
+
|
85
|
+
[Required(ErrorMessage = "param2 は必須")]
|
86
|
+
|
87
|
+
public string param2 { set; get; }
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
[Required(ErrorMessage = "param3 は必須")]
|
92
|
+
|
93
|
+
public string param3 { set; get; }
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
public class Z
|
100
|
+
|
101
|
+
{
|
102
|
+
|
103
|
+
[Required(ErrorMessage = "Title は必須")]
|
104
|
+
|
105
|
+
public string Title { set; get; }
|
106
|
+
|
107
|
+
public A A_Class { set; get; }
|
108
|
+
|
109
|
+
public B B_Class { set; get; }
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
```
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
**Controller**
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
using System;
|
126
|
+
|
127
|
+
using System.Collections.Generic;
|
128
|
+
|
129
|
+
using System.Linq;
|
130
|
+
|
131
|
+
using System.Web;
|
132
|
+
|
133
|
+
using System.Web.Mvc;
|
134
|
+
|
135
|
+
using Mvc4App.Models;
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
namespace Mvc4App.Controllers
|
140
|
+
|
141
|
+
{
|
142
|
+
|
143
|
+
public class ParentChildController : Controller
|
144
|
+
|
145
|
+
[HttpGet]
|
146
|
+
|
147
|
+
public ActionResult Create3()
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
return View();
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
[HttpPost]
|
158
|
+
|
159
|
+
public ActionResult Create3(Z z, A a_class, B b_class)
|
160
|
+
|
161
|
+
{
|
162
|
+
|
163
|
+
if (!ModelState.IsValid)
|
164
|
+
|
165
|
+
{
|
166
|
+
|
167
|
+
return View(z);
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
return RedirectToAction("Index", "Home");
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
```
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
**View**
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
```
|
190
|
+
|
191
|
+
@model Mvc4App.Models.Z
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
@{
|
196
|
+
|
197
|
+
ViewBag.Title = "Create3";
|
198
|
+
|
199
|
+
Layout = "~/Views/Shared/_Layout.cshtml";
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
<h2>Create3</h2>
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
@using (Html.BeginForm())
|
210
|
+
|
211
|
+
{
|
212
|
+
|
213
|
+
@Html.LabelFor(m => m.Title)
|
214
|
+
|
215
|
+
@Html.EditorFor(m => m.Title)
|
216
|
+
|
217
|
+
@Html.ValidationMessageFor(m => m.Title)
|
218
|
+
|
219
|
+
<hr />
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
@Html.LabelFor(m => m.A_Class.param1)
|
224
|
+
|
225
|
+
@Html.EditorFor(m => m.A_Class.param1)
|
226
|
+
|
227
|
+
@Html.ValidationMessageFor(m => m.A_Class.param1)
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
<hr />
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
@Html.LabelFor(m => m.B_Class.param2)
|
236
|
+
|
237
|
+
@Html.EditorFor(m => m.B_Class.param2)
|
238
|
+
|
239
|
+
@Html.ValidationMessageFor(m => m.B_Class.param2)
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
@Html.LabelFor(m => m.B_Class.param3)
|
244
|
+
|
245
|
+
@Html.EditorFor(m => m.B_Class.param3)
|
246
|
+
|
247
|
+
@Html.ValidationMessageFor(m => m.B_Class.param3)
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
<hr />
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
<input type="submit" value="POST" />
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
@section Scripts {
|
262
|
+
|
263
|
+
@Scripts.Render("~/bundles/jqueryval")
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
```
|