質問編集履歴
1
ソースコードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -27,8 +27,211 @@
|
|
27
27
|
|
28
28
|
このとき、AnimalもしくはPlantのページで追加したデータや初期データをViewModelのページに反映させる、またLivingViewModelの方で編集したデータをAnimalもしくはPlantのページに反映させるControllerの書き方がわかりません。
|
29
29
|
|
30
|
+
以下AnimalのControllerです。
|
31
|
+
```C#
|
32
|
+
using System;
|
33
|
+
using System.Collections.Generic;
|
34
|
+
using System.Linq;
|
35
|
+
using System.Threading.Tasks;
|
36
|
+
using Microsoft.AspNetCore.Mvc;
|
37
|
+
using Microsoft.AspNetCore.Mvc.Rendering;
|
38
|
+
using Microsoft.EntityFrameworkCore;
|
39
|
+
using WebApplication.Data;
|
40
|
+
using WebApplication.Models;
|
41
|
+
|
42
|
+
namespace WebApplication.Controllers
|
43
|
+
{
|
44
|
+
public class AnimalsController : Controller
|
45
|
+
{
|
46
|
+
private readonly MyDbContext _context;
|
47
|
+
|
48
|
+
public AnimalsController(MyDbContext context)
|
49
|
+
{
|
50
|
+
_context = context;
|
51
|
+
}
|
52
|
+
|
53
|
+
// GET: Animals
|
54
|
+
public async Task<IActionResult> Index()
|
55
|
+
{
|
56
|
+
return View(await _context.Animals.ToListAsync());
|
57
|
+
}
|
58
|
+
|
59
|
+
// GET: Animals/Details/5
|
60
|
+
public async Task<IActionResult> Details(int? id)
|
61
|
+
{
|
62
|
+
if (id == null)
|
63
|
+
{
|
64
|
+
return NotFound();
|
65
|
+
}
|
66
|
+
|
67
|
+
var Animal = await _context.Animals
|
68
|
+
.FirstOrDefaultAsync(m => m.Id == id);
|
69
|
+
if (Animal == null)
|
70
|
+
{
|
71
|
+
return NotFound();
|
72
|
+
}
|
73
|
+
|
74
|
+
return View(Animal);
|
75
|
+
}
|
76
|
+
|
77
|
+
// GET: Animals/Create
|
78
|
+
public IActionResult Create()
|
79
|
+
{
|
80
|
+
return View();
|
81
|
+
}
|
82
|
+
|
83
|
+
// POST: Animals/Create
|
84
|
+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
85
|
+
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
86
|
+
[HttpPost]
|
87
|
+
[ValidateAntiForgeryToken]
|
88
|
+
public async Task<IActionResult> Create([Bind("Id,Name")] Animal Animal)
|
89
|
+
{
|
90
|
+
if (ModelState.IsValid)
|
91
|
+
{
|
92
|
+
_context.Add(Animal);
|
93
|
+
await _context.SaveChangesAsync();
|
94
|
+
return RedirectToAction(nameof(Index));
|
95
|
+
}
|
96
|
+
return View(Animal);
|
97
|
+
}
|
98
|
+
|
99
|
+
// GET: Animals/Edit/5
|
100
|
+
public async Task<IActionResult> Edit(int? id)
|
101
|
+
{
|
102
|
+
if (id == null)
|
103
|
+
{
|
104
|
+
return NotFound();
|
105
|
+
}
|
106
|
+
|
107
|
+
var Animal = await _context.Animals.FindAsync(id);
|
108
|
+
if (Animal == null)
|
109
|
+
{
|
110
|
+
return NotFound();
|
111
|
+
}
|
112
|
+
return View(Animal);
|
113
|
+
}
|
114
|
+
|
115
|
+
// POST: Animals/Edit/5
|
116
|
+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
117
|
+
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
118
|
+
[HttpPost]
|
119
|
+
[ValidateAntiForgeryToken]
|
120
|
+
public async Task<IActionResult> Edit(int id, [Bind("Id,Name")] Animal Animal)
|
121
|
+
{
|
122
|
+
if (id != Animal.Id)
|
123
|
+
{
|
124
|
+
return NotFound();
|
125
|
+
}
|
126
|
+
|
127
|
+
if (ModelState.IsValid)
|
128
|
+
{
|
129
|
+
try
|
130
|
+
{
|
131
|
+
_context.Update(Animal);
|
132
|
+
await _context.SaveChangesAsync();
|
133
|
+
}
|
134
|
+
catch (DbUpdateConcurrencyException)
|
135
|
+
{
|
136
|
+
if (!AnimalExists(Animal.Id))
|
137
|
+
{
|
138
|
+
return NotFound();
|
139
|
+
}
|
140
|
+
else
|
141
|
+
{
|
142
|
+
throw;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
return RedirectToAction(nameof(Index));
|
146
|
+
}
|
147
|
+
return View(Animal);
|
148
|
+
}
|
149
|
+
|
150
|
+
// GET: Animals/Delete/5
|
151
|
+
public async Task<IActionResult> Delete(int? id)
|
152
|
+
{
|
153
|
+
if (id == null)
|
154
|
+
{
|
155
|
+
return NotFound();
|
156
|
+
}
|
157
|
+
|
158
|
+
var Animal = await _context.Animals
|
159
|
+
.FirstOrDefaultAsync(m => m.Id == id);
|
160
|
+
if (Animal == null)
|
161
|
+
{
|
162
|
+
return NotFound();
|
163
|
+
}
|
164
|
+
|
165
|
+
return View(Animal);
|
166
|
+
}
|
167
|
+
|
168
|
+
// POST: Animals/Delete/5
|
169
|
+
[HttpPost, ActionName("Delete")]
|
170
|
+
[ValidateAntiForgeryToken]
|
171
|
+
public async Task<IActionResult> DeleteConfirmed(int id)
|
172
|
+
{
|
173
|
+
var Animal = await _context.Animals.FindAsync(id);
|
174
|
+
_context.Animals.Remove(Animal);
|
175
|
+
await _context.SaveChangesAsync();
|
176
|
+
return RedirectToAction(nameof(Index));
|
177
|
+
}
|
178
|
+
|
179
|
+
private bool AnimalExists(int id)
|
180
|
+
{
|
181
|
+
return _context.Animals.Any(e => e.Id == id);
|
182
|
+
}
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
```
|
187
|
+
|
30
188
|
AnimalやPlantのデータとは関係なくLivingViewModelに初期データを追加することは出来るのですが、相互に作用できるようにしたいです。
|
31
189
|
|
190
|
+
以下現在のViewModelのControllerです。
|
191
|
+
```C#
|
192
|
+
using System;
|
193
|
+
using System.Collections.Generic;
|
194
|
+
using System.Linq;
|
195
|
+
using System.Threading.Tasks;
|
196
|
+
using Microsoft.AspNetCore.Mvc;
|
197
|
+
using Microsoft.EntityFrameworkCore;
|
198
|
+
using WebApplication.Data;
|
199
|
+
using WebApplication.Models;
|
200
|
+
|
201
|
+
namespace WebApplication.Controllers
|
202
|
+
{
|
203
|
+
public class LivingViewModelsController : Controller
|
204
|
+
{
|
205
|
+
|
206
|
+
public IActionResult Index()
|
207
|
+
{
|
208
|
+
LivingViewModel myView = new LivingViewModel();
|
209
|
+
myView.Animals = GetAnimals();
|
210
|
+
myView.Plants = GetPlants();
|
211
|
+
return View(myView);
|
212
|
+
}
|
213
|
+
|
214
|
+
private List<Animal> GetAnimals()
|
215
|
+
{
|
216
|
+
List<Animal> Animals = new List<Animal>();
|
217
|
+
Animals.Add(new Animal { /*初期データ*/ });
|
218
|
+
return Animals;
|
219
|
+
|
220
|
+
}
|
221
|
+
|
222
|
+
private List<Plant> GetPlants()
|
223
|
+
{
|
224
|
+
List<Plant> Plants = new List<Plant>();
|
225
|
+
Plants.Add(new Plant { /*初期データ*/ });
|
226
|
+
|
227
|
+
return Plants;
|
228
|
+
}
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
```
|
233
|
+
|
234
|
+
|
32
235
|
DbContext、DbInitializerは以下の通りです。
|
33
236
|
```C#
|
34
237
|
using System;
|