回答編集履歴
1
追記
test
CHANGED
@@ -21,3 +21,413 @@
|
|
21
21
|
|
22
22
|
|
23
23
|
と、言葉で書くだけでは分からないと思いますので、あとでサンプルコードを書いてアップします。お待ちください。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
**【追記】**
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
以下、サンプルです。
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
**Controller / Action Method / Model**
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
using System;
|
42
|
+
|
43
|
+
using System.Collections.Generic;
|
44
|
+
|
45
|
+
using System.Linq;
|
46
|
+
|
47
|
+
using System.Web;
|
48
|
+
|
49
|
+
using System.Web.Mvc;
|
50
|
+
|
51
|
+
using System.ComponentModel.DataAnnotations;
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
namespace Mvc5App.Controllers
|
56
|
+
|
57
|
+
{
|
58
|
+
|
59
|
+
public class HomeController : Controller
|
60
|
+
|
61
|
+
{
|
62
|
+
|
63
|
+
public ActionResult Index()
|
64
|
+
|
65
|
+
{
|
66
|
+
|
67
|
+
return View();
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
[HttpGet]
|
74
|
+
|
75
|
+
public ActionResult QuestionsAndAnswers()
|
76
|
+
|
77
|
+
{
|
78
|
+
|
79
|
+
// サンプル用の QAModels を作成
|
80
|
+
|
81
|
+
QAModels model = new QAModels
|
82
|
+
|
83
|
+
{
|
84
|
+
|
85
|
+
QA_Id = 1,
|
86
|
+
|
87
|
+
Employee_Id = 12345,
|
88
|
+
|
89
|
+
Remarks = "備考",
|
90
|
+
|
91
|
+
Details = new List<QADetailModels>
|
92
|
+
|
93
|
+
{
|
94
|
+
|
95
|
+
// 質問数は任意。ここではとりあえず 5 題としています。
|
96
|
+
|
97
|
+
new QADetailModels { QA_Id = 1, Employee_Id = 12345, QA_No = 1, QA_Description = "質問文その 1" },
|
98
|
+
|
99
|
+
new QADetailModels { QA_Id = 1, Employee_Id = 12345, QA_No = 2, QA_Description = "質問文その 2" },
|
100
|
+
|
101
|
+
new QADetailModels { QA_Id = 1, Employee_Id = 12345, QA_No = 3, QA_Description = "質問文その 3" },
|
102
|
+
|
103
|
+
new QADetailModels { QA_Id = 1, Employee_Id = 12345, QA_No = 4, QA_Description = "質問文その 4" },
|
104
|
+
|
105
|
+
new QADetailModels { QA_Id = 1, Employee_Id = 12345, QA_No = 5, QA_Description = "質問文その 5" }
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
};
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
return View(model);
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
[HttpPost]
|
120
|
+
|
121
|
+
[ValidateAntiForgeryToken]
|
122
|
+
|
123
|
+
public ActionResult QuestionsAndAnswers(QAModels model)
|
124
|
+
|
125
|
+
{
|
126
|
+
|
127
|
+
return View(model);
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
// Model
|
136
|
+
|
137
|
+
// Controller に含めたのは単に分けるのが面倒だったからです
|
138
|
+
|
139
|
+
// 質問者さんのコードの DisplayName は古いので以下のように変えました
|
140
|
+
|
141
|
+
// それ以外は質問者さんのコードの通りです。
|
142
|
+
|
143
|
+
public class QAModels
|
144
|
+
|
145
|
+
{
|
146
|
+
|
147
|
+
[Display(Name="ID")]
|
148
|
+
|
149
|
+
public int QA_Id { get; set; }
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
[Display(Name="社員番号")]
|
154
|
+
|
155
|
+
public int Employee_Id { get; set; }
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
[Display(Name="備考")]
|
160
|
+
|
161
|
+
public string Remarks { get; set; }
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
public IList<QADetailModels> Details { get; set; }
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
public class QADetailModels
|
172
|
+
|
173
|
+
{
|
174
|
+
|
175
|
+
[Display(Name="ID")]
|
176
|
+
|
177
|
+
public int QA_Id { get; set; }
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
[Display(Name="社員番号")]
|
182
|
+
|
183
|
+
public int Employee_Id { get; set; }
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
[Display(Name="アンケートNo")]
|
188
|
+
|
189
|
+
public short QA_No { get; set; }
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
// このアンケート内容は別のテーブルから引いてくるものとする
|
194
|
+
|
195
|
+
[Display(Name="アンケート内容")]
|
196
|
+
|
197
|
+
public string QA_Description { get; set; }
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
[Display(Name="回答")]
|
202
|
+
|
203
|
+
public short QA_Ans { get; set; }
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
**View**
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
上のアクションメソッド QuestionsAndAnswers をベースに Visual Studio 2015 のスキャフォールディング機能を使って自動生成させたものです。Template は Create を、Model class は QAModels を設定。それに <table> ... </table> のコードを追加した以外は手を加えていません。
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
```
|
222
|
+
|
223
|
+
@model Mvc5App.Controllers.QAModels
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
@{
|
228
|
+
|
229
|
+
ViewBag.Title = "QuesttionsAndAnswers";
|
230
|
+
|
231
|
+
Layout = "~/Views/Shared/_Layout.cshtml";
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
<h2>QuesttionsAndAnswers</h2>
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
@using (Html.BeginForm())
|
244
|
+
|
245
|
+
{
|
246
|
+
|
247
|
+
@Html.AntiForgeryToken()
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
<div class="form-horizontal">
|
252
|
+
|
253
|
+
<h4>QAModels</h4>
|
254
|
+
|
255
|
+
<hr />
|
256
|
+
|
257
|
+
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
|
258
|
+
|
259
|
+
<div class="form-group">
|
260
|
+
|
261
|
+
@Html.LabelFor(model => model.QA_Id, htmlAttributes: new { @class = "control-label col-md-2" })
|
262
|
+
|
263
|
+
<div class="col-md-10">
|
264
|
+
|
265
|
+
@Html.EditorFor(model => model.QA_Id, new { htmlAttributes = new { @class = "form-control" } })
|
266
|
+
|
267
|
+
@Html.ValidationMessageFor(model => model.QA_Id, "", new { @class = "text-danger" })
|
268
|
+
|
269
|
+
</div>
|
270
|
+
|
271
|
+
</div>
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
<div class="form-group">
|
276
|
+
|
277
|
+
@Html.LabelFor(model => model.Employee_Id, htmlAttributes: new { @class = "control-label col-md-2" })
|
278
|
+
|
279
|
+
<div class="col-md-10">
|
280
|
+
|
281
|
+
@Html.EditorFor(model => model.Employee_Id, new { htmlAttributes = new { @class = "form-control" } })
|
282
|
+
|
283
|
+
@Html.ValidationMessageFor(model => model.Employee_Id, "", new { @class = "text-danger" })
|
284
|
+
|
285
|
+
</div>
|
286
|
+
|
287
|
+
</div>
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
<div class="form-group">
|
292
|
+
|
293
|
+
@Html.LabelFor(model => model.Remarks, htmlAttributes: new { @class = "control-label col-md-2" })
|
294
|
+
|
295
|
+
<div class="col-md-10">
|
296
|
+
|
297
|
+
@Html.EditorFor(model => model.Remarks, new { htmlAttributes = new { @class = "form-control" } })
|
298
|
+
|
299
|
+
@Html.ValidationMessageFor(model => model.Remarks, "", new { @class = "text-danger" })
|
300
|
+
|
301
|
+
</div>
|
302
|
+
|
303
|
+
</div>
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
<table class="table">
|
308
|
+
|
309
|
+
<tr>
|
310
|
+
|
311
|
+
<th>
|
312
|
+
|
313
|
+
@Html.DisplayNameFor(model => model.Details[0].QA_No)
|
314
|
+
|
315
|
+
</th>
|
316
|
+
|
317
|
+
<th>
|
318
|
+
|
319
|
+
@Html.DisplayNameFor(model => model.Details[0].QA_Description)
|
320
|
+
|
321
|
+
</th>
|
322
|
+
|
323
|
+
<th>
|
324
|
+
|
325
|
+
@Html.DisplayNameFor(model => model.Details[0].QA_Ans)
|
326
|
+
|
327
|
+
</th>
|
328
|
+
|
329
|
+
</tr>
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
@for (int i = 0; i < Model.Details.Count; i++)
|
336
|
+
|
337
|
+
{
|
338
|
+
|
339
|
+
<tr>
|
340
|
+
|
341
|
+
@Html.HiddenFor(model => model.Details[i].QA_Id)
|
342
|
+
|
343
|
+
@Html.HiddenFor(model => model.Details[i].Employee_Id)
|
344
|
+
|
345
|
+
<td>
|
346
|
+
|
347
|
+
@Html.EditorFor(model => model.Details[i].QA_No, new { htmlAttributes = new { @class = "form-control" } })
|
348
|
+
|
349
|
+
</td>
|
350
|
+
|
351
|
+
<td>
|
352
|
+
|
353
|
+
@Html.EditorFor(model => model.Details[i].QA_Description, new { htmlAttributes = new { @class = "form-control" } })
|
354
|
+
|
355
|
+
</td>
|
356
|
+
|
357
|
+
<td>
|
358
|
+
|
359
|
+
@*@Html.EditorFor(model => model.Details[i].QA_Ans, new { htmlAttributes = new { @class = "form-control" } })*@
|
360
|
+
|
361
|
+
@Html.RadioButton("Details[" + i + "].QA_Ans", 5, false)<label>非常に満足 </label>
|
362
|
+
|
363
|
+
@Html.RadioButton("Details[" + i + "].QA_Ans", 4, false)<label>満足 </label>
|
364
|
+
|
365
|
+
@Html.RadioButton("Details[" + i + "].QA_Ans", 3, false)<label>普通 </label>
|
366
|
+
|
367
|
+
@Html.RadioButton("Details[" + i + "].QA_Ans", 2, false)<label>不満 </label>
|
368
|
+
|
369
|
+
@Html.RadioButton("Details[" + i + "].QA_Ans", 1, false)<label>非常に不満 </label>
|
370
|
+
|
371
|
+
</td>
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
</tr>
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
</table>
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
<div class="form-group">
|
384
|
+
|
385
|
+
<div class="col-md-offset-2 col-md-10">
|
386
|
+
|
387
|
+
<input type="submit" value="Create" class="btn btn-default" />
|
388
|
+
|
389
|
+
</div>
|
390
|
+
|
391
|
+
</div>
|
392
|
+
|
393
|
+
</div>
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
<div>
|
400
|
+
|
401
|
+
@Html.ActionLink("Back to List", "Index")
|
402
|
+
|
403
|
+
</div>
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
@section Scripts {
|
408
|
+
|
409
|
+
@Scripts.Render("~/bundles/jqueryval")
|
410
|
+
|
411
|
+
}
|
412
|
+
|
413
|
+
```
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
以下のように RadioButton を設定して[Create]ボタンをクリックするとデータがサーバーに送信されて、
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
![イメージ説明](d3c55cb7f41ca21a6b195be9ac42008c.jpeg)
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
以下の通り public ActionResult QuestionsAndAnswers(QAModels model) の model にモデルバインディングされます。
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
![イメージ説明](d03e7da7c2cfce4d1109ff7458e2f21c.jpeg)
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
以上ですが、分からないところがあったら聞いてください。
|