質問編集履歴

1

回答がない。回答がない。

2017/10/30 13:37

投稿

cancat
cancat

スコア313

test CHANGED
@@ -1 +1 @@
1
- ASP.NET WebアプリケーションのViewとControllerの引数とModelの関係を具体的に知りたです
1
+ 回答がない。回答がない。
test CHANGED
@@ -1,293 +1 @@
1
- こんにちは。
2
-
3
- Windows10でASP.NETのアプリケーションを開発しています。
4
-
5
- Visual Studio 2017 Communityを使っています。
6
-
7
-
8
-
9
- ###前提・実現したいこと
10
-
11
- ASP.NET Webアプリケーション(.NET Framework)のViewとControllerの関係について疑問があります。
12
-
13
- 具体的な疑問に答えていただければと思います。
14
-
15
-
16
-
17
- Create.cshtmlを開いたときはControllerのCreate(),、
18
-
19
- Create.cshtmlでcreateを押したときは、ControllerのCreate([Bind(Include = "Id,Name,email")] Person person)
20
-
21
- が、動きます。
22
-
23
-
24
-
25
- Q1)ASP.NETでないFormやWPFのC#の場合、同じ名前のMethodがふたつある場合、引数をつけて実行するMethodを選択できます。
26
-
27
- Create();と実行すればCreate()を、
28
-
29
- Create(person)と実行すればCreate(person)を実行します。
30
-
31
- とすると、
32
-
33
- createボタンでCreate([Bind(Include = "Id,Name,email")] Person person)を実行するとうことは、createボタンを押したときは、自動的にpersonを引数に取るのでしょうか?
1
+ 回答がな。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。回答がない。
34
-
35
-
36
-
37
- Q2)そうだとすると、この引数のpersonはどこで定義しているのでしょうか?
38
-
39
- Create.cshtmlの1行目の
40
-
41
- @model ConnectionList.Models.Person
42
-
43
- のmodelがそうでしょうか?
44
-
45
-
46
-
47
- Q3)なぜその定義した引数が、
48
-
49
- <input type="submit" value="Create" class="btn btn-default" />
50
-
51
- だけでついていくの?
52
-
53
-
54
-
55
- Q4)するとcshtmlの冒頭では、変数を定義できる、ということ?
56
-
57
-
58
-
59
- Q5)その変数は、cshtmlごとにひとつに限定? それとも複数定義できるの?
60
-
61
-
62
-
63
- Q6)複数定義したときは、Createで受ける引数はふたつになる?
64
-
65
-
66
-
67
- と次々と疑問が噴出してまいりました。
68
-
69
- 具体的な疑問に答えていただければと思います。
70
-
71
- ご助言お願いします。
72
-
73
-
74
-
75
- ###試したこと
76
-
77
- 新規プロジェクトで、ASP.NET Webアプリケーション(.NET Framework)を選択。
78
-
79
- ASP.NET4.5.2テンプレートはMVCを選択。
80
-
81
- 認証は個別のユーザーアカウントを選択。
82
-
83
-
84
-
85
- ModelフォルダにPersonクラスを作成。
86
-
87
- ###発生している問題・エラーメッセージ
88
-
89
-
90
-
91
- ###該当のソースコード
92
-
93
- ```C#
94
-
95
- namespace ConnectionList.Models
96
-
97
- {
98
-
99
- public class Person
100
-
101
- {
102
-
103
- public int Id { get; set; }
104
-
105
- public string Name { get; set; }
106
-
107
- public string email { get; set; }
108
-
109
- }
110
-
111
- }
112
-
113
- ```
114
-
115
-
116
-
117
- Controllersフォルダを選択して、追加-新規スキャフォールディングアイテムを選択。
118
-
119
- MVC-コントローラ-Entity Frameworkを使用した、ビューがあるMVC5コントローラーを選択。
120
-
121
- モデルクラスにPerson(ConnectionList.Models)、データコンテキストクラスにApplicationDbContext(ConnectionList.Models)を選択して追加。
122
-
123
-
124
-
125
- これで、CURDができました。
126
-
127
-
128
-
129
- PeopleController.csのうちCreateの部分は下記のとおりです。
130
-
131
-
132
-
133
- ```C#
134
-
135
- namespace ConnectionList.Controllers
136
-
137
- {
138
-
139
- public class PeopleController : Controller
140
-
141
- {
142
-
143
- // GET: People/Create
144
-
145
- public ActionResult Create()
146
-
147
- {
148
-
149
- return View();
150
-
151
- }
152
-
153
-
154
-
155
- // POST: People/Create
156
-
157
- [HttpPost]
158
-
159
- [ValidateAntiForgeryToken]
160
-
161
- public ActionResult Create([Bind(Include = "Id,Name,email")] Person person)
162
-
163
- {
164
-
165
- if (ModelState.IsValid)
166
-
167
- {
168
-
169
- db.People.Add(person);
170
-
171
- db.SaveChanges();
172
-
173
- return RedirectToAction("Index");
174
-
175
- }
176
-
177
-
178
-
179
- return View(person);
180
-
181
- }
182
-
183
-
184
-
185
- }
186
-
187
- }
188
-
189
- ```
190
-
191
-
192
-
193
- Create.cshtmlの抜粋は下記のとおりです。
194
-
195
- ```html
196
-
197
- @model ConnectionList.Models.Person
198
-
199
-
200
-
201
- @{
202
-
203
- ViewBag.Title = "Create";
204
-
205
- }
206
-
207
-
208
-
209
- <h2>Create</h2>
210
-
211
-
212
-
213
- @using (Html.BeginForm())
214
-
215
- {
216
-
217
- @Html.AntiForgeryToken()
218
-
219
-
220
-
221
- <div class="form-horizontal">
222
-
223
- <h4>Person</h4>
224
-
225
- <hr />
226
-
227
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
228
-
229
- <div class="form-group">
230
-
231
- @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
232
-
233
- <div class="col-md-10">
234
-
235
- @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
236
-
237
- @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
238
-
239
- </div>
240
-
241
- </div>
242
-
243
-
244
-
245
- <div class="form-group">
246
-
247
- @Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
248
-
249
- <div class="col-md-10">
250
-
251
- @Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
252
-
253
- @Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
254
-
255
- </div>
256
-
257
- </div>
258
-
259
-
260
-
261
- <div class="form-group">
262
-
263
- <div class="col-md-offset-2 col-md-10">
264
-
265
- <input type="submit" value="Create" class="btn btn-default" />
266
-
267
- </div>
268
-
269
- </div>
270
-
271
- </div>
272
-
273
- }
274
-
275
- ```
276
-
277
-
278
-
279
- ###補足情報(言語/FW/ツール等のバージョンなど)
280
-
281
- Microsoft Visual Studio Community 2017
282
-
283
- Version 15.0.26228.9 D15RTWSVC
284
-
285
- Microsoft .NET Framework
286
-
287
- Version 4.6.01586
288
-
289
-
290
-
291
- です。
292
-
293
- よろしくお願いします。