質問編集履歴
4
コード追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -74,151 +74,269 @@
|
|
74
74
|
|
75
75
|
|
76
76
|
|
77
|
+
|
78
|
+
|
77
79
|
```C#
|
78
80
|
|
81
|
+
using System;
|
82
|
+
|
83
|
+
using System.Configuration;
|
84
|
+
|
85
|
+
using System.Data.SqlClient;
|
86
|
+
|
87
|
+
using System.Web.Mvc;
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
namespace Forum.Controllers
|
92
|
+
|
93
|
+
{
|
94
|
+
|
95
|
+
public class ForumController : Controller
|
96
|
+
|
97
|
+
{
|
98
|
+
|
99
|
+
// 上述したRazorのコードはNewTopic上にある
|
100
|
+
|
101
|
+
public ActionResult NewTopic()
|
102
|
+
|
103
|
+
{
|
104
|
+
|
105
|
+
return View();
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
// 上述したHtml.ActionLinkの"新規トピックを投稿する"をクリックすると実行する
|
114
|
+
|
115
|
+
public ActionResult NewTopicSubmit()
|
116
|
+
|
117
|
+
{
|
118
|
+
|
119
|
+
|
120
|
+
|
79
|
-
string htmltitle = Request["Title"];
|
121
|
+
string htmltitle = Request["Title"];
|
122
|
+
|
123
|
+
|
124
|
+
|
80
|
-
|
125
|
+
// 接続文字列の取得。
|
126
|
+
|
127
|
+
var connectionString = ConfigurationManager.ConnectionStrings["ForumConnection"].ConnectionString;
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
using (var connection = new SqlConnection(connectionString))
|
134
|
+
|
135
|
+
{
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
try
|
140
|
+
|
141
|
+
{
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
// データベースの接続開始
|
146
|
+
|
147
|
+
connection.Open();
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
using (var transaction = connection.BeginTransaction())
|
152
|
+
|
153
|
+
using (var command = new SqlCommand() { Connection = connection, Transaction = transaction })
|
154
|
+
|
155
|
+
{
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
try
|
160
|
+
|
161
|
+
{
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
// 親テーブルを挿入するSQLの準備
|
166
|
+
|
167
|
+
command.CommandText = @"INSERT INTO Topic (Title) VALUES (@title)";
|
168
|
+
|
169
|
+
command.Parameters.Add(new SqlParameter("@title", htmltitle));
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
// 親テーブルを挿入するSQLの実行
|
174
|
+
|
175
|
+
command.ExecuteNonQuery();
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
// コミット
|
180
|
+
|
181
|
+
transaction.Commit();
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
catch
|
190
|
+
|
191
|
+
{
|
192
|
+
|
193
|
+
// ロールバック
|
194
|
+
|
195
|
+
transaction.Rollback();
|
196
|
+
|
197
|
+
throw;
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
catch (Exception exception)
|
208
|
+
|
209
|
+
{
|
210
|
+
|
211
|
+
Console.WriteLine(exception.Message);
|
212
|
+
|
213
|
+
throw;
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
finally
|
218
|
+
|
219
|
+
{
|
220
|
+
|
221
|
+
// データベースの接続終了
|
222
|
+
|
223
|
+
connection.Close();
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
return View();
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
}
|
236
|
+
|
81
|
-
```
|
237
|
+
```
|
82
|
-
|
83
|
-
|
84
238
|
|
85
239
|
|
86
240
|
|
87
241
|
```C#
|
88
242
|
|
89
|
-
|
243
|
+
using System.Collections.Generic;
|
244
|
+
|
90
|
-
|
245
|
+
using System.Data.Entity;
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
namespace Forum.Models
|
250
|
+
|
251
|
+
{
|
252
|
+
|
253
|
+
public class ForumModels
|
254
|
+
|
255
|
+
{
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
}
|
260
|
+
|
91
|
-
|
261
|
+
public class NewTopic
|
262
|
+
|
263
|
+
{
|
264
|
+
|
265
|
+
public string Nickname { get; set; }
|
266
|
+
|
267
|
+
public string Category { get; set; }
|
268
|
+
|
269
|
+
public string Region { get; set; }
|
270
|
+
|
271
|
+
public string Title { get; set; }
|
272
|
+
|
273
|
+
public string Comment { get; set; }
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
public class TopicTitle
|
280
|
+
|
281
|
+
{
|
282
|
+
|
283
|
+
public int Id { get; set; }
|
284
|
+
|
285
|
+
public string Title { get; set; }
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
public class ForumConnection : DbContext
|
292
|
+
|
293
|
+
{
|
294
|
+
|
295
|
+
public DbSet<TopicTitle> TopicTitles { get; set; }
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
public class MvcBasicInitializer : CreateDatabaseIfNotExists<ForumConnection>
|
302
|
+
|
303
|
+
{
|
304
|
+
|
305
|
+
protected override void Seed(ForumConnection context)
|
92
306
|
|
93
307
|
{
|
94
308
|
|
309
|
+
base.Seed(context);
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
var topics = new List<TopicTitle>
|
314
|
+
|
315
|
+
{
|
316
|
+
|
317
|
+
new TopicTitle
|
318
|
+
|
319
|
+
{
|
320
|
+
|
321
|
+
Id = 4,
|
322
|
+
|
323
|
+
Title ="たいとるー1"
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
};
|
328
|
+
|
329
|
+
topics.ForEach(m => context.TopicTitles.Add(m));
|
330
|
+
|
95
|
-
|
331
|
+
context.SaveChanges();
|
96
332
|
|
97
333
|
}
|
98
334
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
// 上述したHtml.ActionLinkの"新規トピックを投稿する"をクリックすると実行する
|
104
|
-
|
105
|
-
public ActionResult NewTopicSubmit()
|
106
|
-
|
107
|
-
{
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
string htmltitle = Request["Title"];
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
// 接続文字列の取得。
|
116
|
-
|
117
|
-
var connectionString = ConfigurationManager.ConnectionStrings["ForumConnection"].ConnectionString;
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
using (var connection = new SqlConnection(connectionString))
|
124
|
-
|
125
|
-
{
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
try
|
130
|
-
|
131
|
-
{
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
// データベースの接続開始
|
136
|
-
|
137
|
-
connection.Open();
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
using (var transaction = connection.BeginTransaction())
|
142
|
-
|
143
|
-
using (var command = new SqlCommand() { Connection = connection, Transaction = transaction })
|
144
|
-
|
145
|
-
{
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
try
|
150
|
-
|
151
|
-
{
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
// 親テーブルを挿入するSQLの準備
|
156
|
-
|
157
|
-
command.CommandText = @"INSERT INTO Topic (Title) VALUES (@title)";
|
158
|
-
|
159
|
-
command.Parameters.Add(new SqlParameter("@title", htmltitle));
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
// 親テーブルを挿入するSQLの実行
|
164
|
-
|
165
|
-
command.ExecuteNonQuery();
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
// コミット
|
170
|
-
|
171
|
-
transaction.Commit();
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
335
|
+
}
|
178
|
-
|
179
|
-
|
336
|
+
|
180
|
-
|
181
|
-
{
|
182
|
-
|
183
|
-
// ロールバック
|
184
|
-
|
185
|
-
transaction.Rollback();
|
186
|
-
|
187
|
-
throw;
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
337
|
+
}
|
192
|
-
|
193
|
-
|
338
|
+
|
194
|
-
|
195
|
-
|
339
|
+
|
196
|
-
|
197
|
-
catch (Exception exception)
|
198
|
-
|
199
|
-
{
|
200
|
-
|
201
|
-
Console.WriteLine(exception.Message);
|
202
|
-
|
203
|
-
throw;
|
204
|
-
|
205
|
-
}
|
206
|
-
|
207
|
-
finally
|
208
|
-
|
209
|
-
{
|
210
|
-
|
211
|
-
// データベースの接続終了
|
212
|
-
|
213
|
-
connection.Close();
|
214
|
-
|
215
|
-
}
|
216
|
-
|
217
|
-
}
|
218
|
-
|
219
|
-
return View();
|
220
|
-
|
221
|
-
}
|
222
340
|
|
223
341
|
```
|
224
342
|
|
3
コードの抜けを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -86,6 +86,22 @@
|
|
86
86
|
|
87
87
|
```C#
|
88
88
|
|
89
|
+
// 上述したRazorのコードはNewTopic上にある
|
90
|
+
|
91
|
+
public ActionResult NewTopic()
|
92
|
+
|
93
|
+
{
|
94
|
+
|
95
|
+
return View();
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
// 上述したHtml.ActionLinkの"新規トピックを投稿する"をクリックすると実行する
|
104
|
+
|
89
105
|
public ActionResult NewTopicSubmit()
|
90
106
|
|
91
107
|
{
|
2
形式にのとった書き方への修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
C#でASP.NET MVCを用いた掲示板機能を作成しようとしており、新規トピック作成機能の実装で、viewでユーザーが選択した値をDBに登録する機能を実装しようとしています。
|
5
|
+
C#でASP.NET MVC5を用いた掲示板機能を作成しようとしており、新規トピック作成機能の実装で、viewでユーザーが選択した値をDBに登録する機能を実装しようとしています。
|
6
6
|
|
7
7
|
ViewにRazorを用いて記した@HtmlxxxFor群の値をController側のActionResultに送るにはどうしたらよいでしょうか。
|
8
8
|
|
@@ -16,6 +16,18 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
+
追記
|
20
|
+
|
21
|
+
質問欄にていただいたことをもとに詳細を記しました。ご指摘ありがとうございます。
|
22
|
+
|
23
|
+
また、TextBoxForを用いていますが、プログラミング初心者の為有用なものの区別がついてないだけでこだわりはないです。
|
24
|
+
|
25
|
+
そのためEditFor等他の有用な方法でもご教授頂けるだけでたいへん有り難いです。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
19
31
|
### 発生している問題・エラーメッセージ
|
20
32
|
|
21
33
|
|
@@ -42,6 +54,22 @@
|
|
42
54
|
|
43
55
|
)
|
44
56
|
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
@Html.ActionLink(
|
62
|
+
|
63
|
+
"新規トピックを投稿する", // リンク・テキスト
|
64
|
+
|
65
|
+
"NewTopicSubmit", // アクション名
|
66
|
+
|
67
|
+
"Forum" // コントローラー名
|
68
|
+
|
69
|
+
)
|
70
|
+
|
71
|
+
|
72
|
+
|
45
73
|
```
|
46
74
|
|
47
75
|
|
@@ -52,17 +80,131 @@
|
|
52
80
|
|
53
81
|
```
|
54
82
|
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
55
87
|
```C#
|
56
88
|
|
89
|
+
public ActionResult NewTopicSubmit()
|
90
|
+
|
91
|
+
{
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
string htmltitle = Request["Title"];
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
// 接続文字列の取得。
|
100
|
+
|
101
|
+
var connectionString = ConfigurationManager.ConnectionStrings["ForumConnection"].ConnectionString;
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
using (var connection = new SqlConnection(connectionString))
|
108
|
+
|
109
|
+
{
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
try
|
114
|
+
|
115
|
+
{
|
116
|
+
|
117
|
+
|
118
|
+
|
57
|
-
// データベースの接続
|
119
|
+
// データベースの接続開始
|
120
|
+
|
58
|
-
|
121
|
+
connection.Open();
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
using (var transaction = connection.BeginTransaction())
|
126
|
+
|
127
|
+
using (var command = new SqlCommand() { Connection = connection, Transaction = transaction })
|
128
|
+
|
129
|
+
{
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
try
|
134
|
+
|
135
|
+
{
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
// 親テーブルを挿入するSQLの準備
|
140
|
+
|
59
|
-
command.CommandText = @"INSERT INTO Topic (Title) VALUES (@title)";
|
141
|
+
command.CommandText = @"INSERT INTO Topic (Title) VALUES (@title)";
|
60
|
-
|
142
|
+
|
61
|
-
command.Parameters.Add(new SqlParameter("@title", htmltitle));
|
143
|
+
command.Parameters.Add(new SqlParameter("@title", htmltitle));
|
144
|
+
|
145
|
+
|
146
|
+
|
62
|
-
|
147
|
+
// 親テーブルを挿入するSQLの実行
|
148
|
+
|
149
|
+
command.ExecuteNonQuery();
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
// コミット
|
154
|
+
|
155
|
+
transaction.Commit();
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
catch
|
164
|
+
|
165
|
+
{
|
166
|
+
|
167
|
+
// ロールバック
|
168
|
+
|
169
|
+
transaction.Rollback();
|
170
|
+
|
171
|
+
throw;
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
catch (Exception exception)
|
182
|
+
|
183
|
+
{
|
184
|
+
|
185
|
+
Console.WriteLine(exception.Message);
|
186
|
+
|
187
|
+
throw;
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
finally
|
192
|
+
|
193
|
+
{
|
194
|
+
|
195
|
+
// データベースの接続終了
|
196
|
+
|
197
|
+
connection.Close();
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
return View();
|
204
|
+
|
205
|
+
}
|
206
|
+
|
63
|
-
```
|
207
|
+
```
|
64
|
-
|
65
|
-
|
66
208
|
|
67
209
|
|
68
210
|
|
@@ -75,3 +217,11 @@
|
|
75
217
|
### 補足情報(FW/ツールのバージョンなど)
|
76
218
|
|
77
219
|
VS2017
|
220
|
+
|
221
|
+
Windows7
|
222
|
+
|
223
|
+
.Net Framework 4.6.1
|
224
|
+
|
225
|
+
MVC5
|
226
|
+
|
227
|
+
テンプレートは用いてないです。
|
1
表現の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
|
15
|
+
初歩的な質問ではないだろうかということは承知なのですが自力で解決できないため、アドバイスを戴けないだろうかと思い投稿しました。宜しくお願いします。
|
16
16
|
|
17
17
|
|
18
18
|
|