質問編集履歴

1

ソースコードの追加

2020/12/04 06:24

投稿

K-actus
K-actus

スコア22

test CHANGED
File without changes
test CHANGED
@@ -56,10 +56,416 @@
56
56
 
57
57
 
58
58
 
59
+ 以下AnimalのControllerです。
60
+
61
+ ```C#
62
+
63
+ using System;
64
+
65
+ using System.Collections.Generic;
66
+
67
+ using System.Linq;
68
+
69
+ using System.Threading.Tasks;
70
+
71
+ using Microsoft.AspNetCore.Mvc;
72
+
73
+ using Microsoft.AspNetCore.Mvc.Rendering;
74
+
75
+ using Microsoft.EntityFrameworkCore;
76
+
77
+ using WebApplication.Data;
78
+
79
+ using WebApplication.Models;
80
+
81
+
82
+
83
+ namespace WebApplication.Controllers
84
+
85
+ {
86
+
87
+ public class AnimalsController : Controller
88
+
89
+ {
90
+
91
+ private readonly MyDbContext _context;
92
+
93
+
94
+
95
+ public AnimalsController(MyDbContext context)
96
+
97
+ {
98
+
99
+ _context = context;
100
+
101
+ }
102
+
103
+
104
+
105
+ // GET: Animals
106
+
107
+ public async Task<IActionResult> Index()
108
+
109
+ {
110
+
111
+ return View(await _context.Animals.ToListAsync());
112
+
113
+ }
114
+
115
+
116
+
117
+ // GET: Animals/Details/5
118
+
119
+ public async Task<IActionResult> Details(int? id)
120
+
121
+ {
122
+
123
+ if (id == null)
124
+
125
+ {
126
+
127
+ return NotFound();
128
+
129
+ }
130
+
131
+
132
+
133
+ var Animal = await _context.Animals
134
+
135
+ .FirstOrDefaultAsync(m => m.Id == id);
136
+
137
+ if (Animal == null)
138
+
139
+ {
140
+
141
+ return NotFound();
142
+
143
+ }
144
+
145
+
146
+
147
+ return View(Animal);
148
+
149
+ }
150
+
151
+
152
+
153
+ // GET: Animals/Create
154
+
155
+ public IActionResult Create()
156
+
157
+ {
158
+
159
+ return View();
160
+
161
+ }
162
+
163
+
164
+
165
+ // POST: Animals/Create
166
+
167
+ // To protect from overposting attacks, please enable the specific properties you want to bind to, for
168
+
169
+ // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
170
+
171
+ [HttpPost]
172
+
173
+ [ValidateAntiForgeryToken]
174
+
175
+ public async Task<IActionResult> Create([Bind("Id,Name")] Animal Animal)
176
+
177
+ {
178
+
179
+ if (ModelState.IsValid)
180
+
181
+ {
182
+
183
+ _context.Add(Animal);
184
+
185
+ await _context.SaveChangesAsync();
186
+
187
+ return RedirectToAction(nameof(Index));
188
+
189
+ }
190
+
191
+ return View(Animal);
192
+
193
+ }
194
+
195
+
196
+
197
+ // GET: Animals/Edit/5
198
+
199
+ public async Task<IActionResult> Edit(int? id)
200
+
201
+ {
202
+
203
+ if (id == null)
204
+
205
+ {
206
+
207
+ return NotFound();
208
+
209
+ }
210
+
211
+
212
+
213
+ var Animal = await _context.Animals.FindAsync(id);
214
+
215
+ if (Animal == null)
216
+
217
+ {
218
+
219
+ return NotFound();
220
+
221
+ }
222
+
223
+ return View(Animal);
224
+
225
+ }
226
+
227
+
228
+
229
+ // POST: Animals/Edit/5
230
+
231
+ // To protect from overposting attacks, please enable the specific properties you want to bind to, for
232
+
233
+ // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
234
+
235
+ [HttpPost]
236
+
237
+ [ValidateAntiForgeryToken]
238
+
239
+ public async Task<IActionResult> Edit(int id, [Bind("Id,Name")] Animal Animal)
240
+
241
+ {
242
+
243
+ if (id != Animal.Id)
244
+
245
+ {
246
+
247
+ return NotFound();
248
+
249
+ }
250
+
251
+
252
+
253
+ if (ModelState.IsValid)
254
+
255
+ {
256
+
257
+ try
258
+
259
+ {
260
+
261
+ _context.Update(Animal);
262
+
263
+ await _context.SaveChangesAsync();
264
+
265
+ }
266
+
267
+ catch (DbUpdateConcurrencyException)
268
+
269
+ {
270
+
271
+ if (!AnimalExists(Animal.Id))
272
+
273
+ {
274
+
275
+ return NotFound();
276
+
277
+ }
278
+
279
+ else
280
+
281
+ {
282
+
283
+ throw;
284
+
285
+ }
286
+
287
+ }
288
+
289
+ return RedirectToAction(nameof(Index));
290
+
291
+ }
292
+
293
+ return View(Animal);
294
+
295
+ }
296
+
297
+
298
+
299
+ // GET: Animals/Delete/5
300
+
301
+ public async Task<IActionResult> Delete(int? id)
302
+
303
+ {
304
+
305
+ if (id == null)
306
+
307
+ {
308
+
309
+ return NotFound();
310
+
311
+ }
312
+
313
+
314
+
315
+ var Animal = await _context.Animals
316
+
317
+ .FirstOrDefaultAsync(m => m.Id == id);
318
+
319
+ if (Animal == null)
320
+
321
+ {
322
+
323
+ return NotFound();
324
+
325
+ }
326
+
327
+
328
+
329
+ return View(Animal);
330
+
331
+ }
332
+
333
+
334
+
335
+ // POST: Animals/Delete/5
336
+
337
+ [HttpPost, ActionName("Delete")]
338
+
339
+ [ValidateAntiForgeryToken]
340
+
341
+ public async Task<IActionResult> DeleteConfirmed(int id)
342
+
343
+ {
344
+
345
+ var Animal = await _context.Animals.FindAsync(id);
346
+
347
+ _context.Animals.Remove(Animal);
348
+
349
+ await _context.SaveChangesAsync();
350
+
351
+ return RedirectToAction(nameof(Index));
352
+
353
+ }
354
+
355
+
356
+
357
+ private bool AnimalExists(int id)
358
+
359
+ {
360
+
361
+ return _context.Animals.Any(e => e.Id == id);
362
+
363
+ }
364
+
365
+ }
366
+
367
+ }
368
+
369
+
370
+
371
+ ```
372
+
373
+
374
+
59
375
  AnimalやPlantのデータとは関係なくLivingViewModelに初期データを追加することは出来るのですが、相互に作用できるようにしたいです。
60
376
 
61
377
 
62
378
 
379
+ 以下現在のViewModelのControllerです。
380
+
381
+ ```C#
382
+
383
+ using System;
384
+
385
+ using System.Collections.Generic;
386
+
387
+ using System.Linq;
388
+
389
+ using System.Threading.Tasks;
390
+
391
+ using Microsoft.AspNetCore.Mvc;
392
+
393
+ using Microsoft.EntityFrameworkCore;
394
+
395
+ using WebApplication.Data;
396
+
397
+ using WebApplication.Models;
398
+
399
+
400
+
401
+ namespace WebApplication.Controllers
402
+
403
+ {
404
+
405
+ public class LivingViewModelsController : Controller
406
+
407
+ {
408
+
409
+
410
+
411
+ public IActionResult Index()
412
+
413
+ {
414
+
415
+ LivingViewModel myView = new LivingViewModel();
416
+
417
+ myView.Animals = GetAnimals();
418
+
419
+ myView.Plants = GetPlants();
420
+
421
+ return View(myView);
422
+
423
+ }
424
+
425
+
426
+
427
+ private List<Animal> GetAnimals()
428
+
429
+ {
430
+
431
+ List<Animal> Animals = new List<Animal>();
432
+
433
+ Animals.Add(new Animal { /*初期データ*/ });
434
+
435
+ return Animals;
436
+
437
+
438
+
439
+ }
440
+
441
+
442
+
443
+ private List<Plant> GetPlants()
444
+
445
+ {
446
+
447
+ List<Plant> Plants = new List<Plant>();
448
+
449
+ Plants.Add(new Plant { /*初期データ*/ });
450
+
451
+
452
+
453
+ return Plants;
454
+
455
+ }
456
+
457
+ }
458
+
459
+ }
460
+
461
+
462
+
463
+ ```
464
+
465
+
466
+
467
+
468
+
63
469
  DbContext、DbInitializerは以下の通りです。
64
470
 
65
471
  ```C#