teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2018/10/12 03:37

投稿

退会済みユーザー
answer CHANGED
@@ -9,4 +9,209 @@
9
9
 
10
10
  あと、モデルに定義されているすべての項目の値が POST されるようにします。なので ```<td>@item.QA_No</td>``` というような形ではなく、EditorFor を使うか、@item.QA_No とか DisplayFor を使うのであれば HiddenFor を追加してください。
11
11
 
12
- と、言葉で書くだけでは分からないと思いますので、あとでサンプルコードを書いてアップします。お待ちください。
12
+ と、言葉で書くだけでは分からないと思いますので、あとでサンプルコードを書いてアップします。お待ちください。
13
+
14
+ **【追記】**
15
+
16
+ 以下、サンプルです。
17
+
18
+ **Controller / Action Method / Model**
19
+
20
+ ```
21
+ using System;
22
+ using System.Collections.Generic;
23
+ using System.Linq;
24
+ using System.Web;
25
+ using System.Web.Mvc;
26
+ using System.ComponentModel.DataAnnotations;
27
+
28
+ namespace Mvc5App.Controllers
29
+ {
30
+ public class HomeController : Controller
31
+ {
32
+ public ActionResult Index()
33
+ {
34
+ return View();
35
+ }
36
+
37
+ [HttpGet]
38
+ public ActionResult QuestionsAndAnswers()
39
+ {
40
+ // サンプル用の QAModels を作成
41
+ QAModels model = new QAModels
42
+ {
43
+ QA_Id = 1,
44
+ Employee_Id = 12345,
45
+ Remarks = "備考",
46
+ Details = new List<QADetailModels>
47
+ {
48
+ // 質問数は任意。ここではとりあえず 5 題としています。
49
+ new QADetailModels { QA_Id = 1, Employee_Id = 12345, QA_No = 1, QA_Description = "質問文その 1" },
50
+ new QADetailModels { QA_Id = 1, Employee_Id = 12345, QA_No = 2, QA_Description = "質問文その 2" },
51
+ new QADetailModels { QA_Id = 1, Employee_Id = 12345, QA_No = 3, QA_Description = "質問文その 3" },
52
+ new QADetailModels { QA_Id = 1, Employee_Id = 12345, QA_No = 4, QA_Description = "質問文その 4" },
53
+ new QADetailModels { QA_Id = 1, Employee_Id = 12345, QA_No = 5, QA_Description = "質問文その 5" }
54
+ }
55
+ };
56
+
57
+ return View(model);
58
+ }
59
+
60
+ [HttpPost]
61
+ [ValidateAntiForgeryToken]
62
+ public ActionResult QuestionsAndAnswers(QAModels model)
63
+ {
64
+ return View(model);
65
+ }
66
+ }
67
+
68
+ // Model
69
+ // Controller に含めたのは単に分けるのが面倒だったからです
70
+ // 質問者さんのコードの DisplayName は古いので以下のように変えました
71
+ // それ以外は質問者さんのコードの通りです。
72
+ public class QAModels
73
+ {
74
+ [Display(Name="ID")]
75
+ public int QA_Id { get; set; }
76
+
77
+ [Display(Name="社員番号")]
78
+ public int Employee_Id { get; set; }
79
+
80
+ [Display(Name="備考")]
81
+ public string Remarks { get; set; }
82
+
83
+ public IList<QADetailModels> Details { get; set; }
84
+ }
85
+
86
+ public class QADetailModels
87
+ {
88
+ [Display(Name="ID")]
89
+ public int QA_Id { get; set; }
90
+
91
+ [Display(Name="社員番号")]
92
+ public int Employee_Id { get; set; }
93
+
94
+ [Display(Name="アンケートNo")]
95
+ public short QA_No { get; set; }
96
+
97
+ // このアンケート内容は別のテーブルから引いてくるものとする
98
+ [Display(Name="アンケート内容")]
99
+ public string QA_Description { get; set; }
100
+
101
+ [Display(Name="回答")]
102
+ public short QA_Ans { get; set; }
103
+ }
104
+ }
105
+ ```
106
+
107
+ **View**
108
+
109
+ 上のアクションメソッド QuestionsAndAnswers をベースに Visual Studio 2015 のスキャフォールディング機能を使って自動生成させたものです。Template は Create を、Model class は QAModels を設定。それに <table> ... </table> のコードを追加した以外は手を加えていません。
110
+
111
+ ```
112
+ @model Mvc5App.Controllers.QAModels
113
+
114
+ @{
115
+ ViewBag.Title = "QuesttionsAndAnswers";
116
+ Layout = "~/Views/Shared/_Layout.cshtml";
117
+ }
118
+
119
+ <h2>QuesttionsAndAnswers</h2>
120
+
121
+
122
+ @using (Html.BeginForm())
123
+ {
124
+ @Html.AntiForgeryToken()
125
+
126
+ <div class="form-horizontal">
127
+ <h4>QAModels</h4>
128
+ <hr />
129
+ @Html.ValidationSummary(true, "", new { @class = "text-danger" })
130
+ <div class="form-group">
131
+ @Html.LabelFor(model => model.QA_Id, htmlAttributes: new { @class = "control-label col-md-2" })
132
+ <div class="col-md-10">
133
+ @Html.EditorFor(model => model.QA_Id, new { htmlAttributes = new { @class = "form-control" } })
134
+ @Html.ValidationMessageFor(model => model.QA_Id, "", new { @class = "text-danger" })
135
+ </div>
136
+ </div>
137
+
138
+ <div class="form-group">
139
+ @Html.LabelFor(model => model.Employee_Id, htmlAttributes: new { @class = "control-label col-md-2" })
140
+ <div class="col-md-10">
141
+ @Html.EditorFor(model => model.Employee_Id, new { htmlAttributes = new { @class = "form-control" } })
142
+ @Html.ValidationMessageFor(model => model.Employee_Id, "", new { @class = "text-danger" })
143
+ </div>
144
+ </div>
145
+
146
+ <div class="form-group">
147
+ @Html.LabelFor(model => model.Remarks, htmlAttributes: new { @class = "control-label col-md-2" })
148
+ <div class="col-md-10">
149
+ @Html.EditorFor(model => model.Remarks, new { htmlAttributes = new { @class = "form-control" } })
150
+ @Html.ValidationMessageFor(model => model.Remarks, "", new { @class = "text-danger" })
151
+ </div>
152
+ </div>
153
+
154
+ <table class="table">
155
+ <tr>
156
+ <th>
157
+ @Html.DisplayNameFor(model => model.Details[0].QA_No)
158
+ </th>
159
+ <th>
160
+ @Html.DisplayNameFor(model => model.Details[0].QA_Description)
161
+ </th>
162
+ <th>
163
+ @Html.DisplayNameFor(model => model.Details[0].QA_Ans)
164
+ </th>
165
+ </tr>
166
+
167
+
168
+ @for (int i = 0; i < Model.Details.Count; i++)
169
+ {
170
+ <tr>
171
+ @Html.HiddenFor(model => model.Details[i].QA_Id)
172
+ @Html.HiddenFor(model => model.Details[i].Employee_Id)
173
+ <td>
174
+ @Html.EditorFor(model => model.Details[i].QA_No, new { htmlAttributes = new { @class = "form-control" } })
175
+ </td>
176
+ <td>
177
+ @Html.EditorFor(model => model.Details[i].QA_Description, new { htmlAttributes = new { @class = "form-control" } })
178
+ </td>
179
+ <td>
180
+ @*@Html.EditorFor(model => model.Details[i].QA_Ans, new { htmlAttributes = new { @class = "form-control" } })*@
181
+ @Html.RadioButton("Details[" + i + "].QA_Ans", 5, false)<label>非常に満足&nbsp;&nbsp;</label>
182
+ @Html.RadioButton("Details[" + i + "].QA_Ans", 4, false)<label>満足&nbsp;&nbsp;</label>
183
+ @Html.RadioButton("Details[" + i + "].QA_Ans", 3, false)<label>普通&nbsp;&nbsp;</label>
184
+ @Html.RadioButton("Details[" + i + "].QA_Ans", 2, false)<label>不満&nbsp;&nbsp;</label>
185
+ @Html.RadioButton("Details[" + i + "].QA_Ans", 1, false)<label>非常に不満&nbsp;&nbsp;</label>
186
+ </td>
187
+
188
+ </tr>
189
+ }
190
+ </table>
191
+
192
+ <div class="form-group">
193
+ <div class="col-md-offset-2 col-md-10">
194
+ <input type="submit" value="Create" class="btn btn-default" />
195
+ </div>
196
+ </div>
197
+ </div>
198
+ }
199
+
200
+ <div>
201
+ @Html.ActionLink("Back to List", "Index")
202
+ </div>
203
+
204
+ @section Scripts {
205
+ @Scripts.Render("~/bundles/jqueryval")
206
+ }
207
+ ```
208
+
209
+ 以下のように RadioButton を設定して[Create]ボタンをクリックするとデータがサーバーに送信されて、
210
+
211
+ ![イメージ説明](d3c55cb7f41ca21a6b195be9ac42008c.jpeg)
212
+
213
+ 以下の通り public ActionResult QuestionsAndAnswers(QAModels model) の model にモデルバインディングされます。
214
+
215
+ ![イメージ説明](d03e7da7c2cfce4d1109ff7458e2f21c.jpeg)
216
+
217
+ 以上ですが、分からないところがあったら聞いてください。