質問編集履歴
4
文の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -164,7 +164,7 @@
|
|
164
164
|
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
165
165
|
[HttpPost]
|
166
166
|
[ValidateAntiForgeryToken]
|
167
|
-
public async Task<IActionResult> Create([Bind("Id,
|
167
|
+
public async Task<IActionResult> Create([Bind("Id, hoge")] hogeList hogeList)
|
168
168
|
{
|
169
169
|
if (ModelState.IsValid)
|
170
170
|
{
|
@@ -196,7 +196,7 @@
|
|
196
196
|
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
197
197
|
[HttpPost]
|
198
198
|
[ValidateAntiForgeryToken]
|
199
|
-
public async Task<IActionResult> Edit(int id, [Bind("Id,
|
199
|
+
public async Task<IActionResult> Edit(int id, [Bind("Id, hoge")] hogeList hogeList)
|
200
200
|
{
|
201
201
|
if (id != hogeList.Id)
|
202
202
|
{
|
3
ソースコード、エラーの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -107,13 +107,176 @@
|
|
107
107
|
```
|
108
108
|
|
109
109
|
hogeListのView及びControllerはScaffoldingで作成しました。
|
110
|
+
```Controller
|
111
|
+
using System;
|
112
|
+
using System.Collections.Generic;
|
113
|
+
using System.Linq;
|
114
|
+
using System.Threading.Tasks;
|
115
|
+
using Microsoft.AspNetCore.Mvc;
|
116
|
+
using Microsoft.AspNetCore.Mvc.Rendering;
|
117
|
+
using Microsoft.EntityFrameworkCore;
|
118
|
+
using DBAccessSample.Data;
|
119
|
+
using DBAccessSample.Models;
|
110
120
|
|
121
|
+
namespace DBAccessSample.Controllers
|
122
|
+
{
|
123
|
+
public class hogeListsController : Controller
|
124
|
+
{
|
125
|
+
private readonly MyDbContext _context;
|
126
|
+
|
127
|
+
public hogeListsController(MyDbContext context)
|
128
|
+
{
|
129
|
+
_context = context;
|
130
|
+
}
|
131
|
+
|
132
|
+
// GET: hogeLists
|
133
|
+
public async Task<IActionResult> Index()
|
134
|
+
{
|
135
|
+
return View(await _context.hogeLists.ToListAsync());
|
136
|
+
}
|
137
|
+
|
138
|
+
// GET: hogeLists/Details/5
|
139
|
+
public async Task<IActionResult> Details(int? id)
|
140
|
+
{
|
141
|
+
if (id == null)
|
142
|
+
{
|
143
|
+
return NotFound();
|
144
|
+
}
|
145
|
+
|
146
|
+
var hogeList = await _context.hogeLists
|
147
|
+
.FirstOrDefaultAsync(m => m.Id == id);
|
148
|
+
if (hogeList == null)
|
149
|
+
{
|
150
|
+
return NotFound();
|
151
|
+
}
|
152
|
+
|
153
|
+
return View(hogeList);
|
154
|
+
}
|
155
|
+
|
156
|
+
// GET: hogeLists/Create
|
157
|
+
public IActionResult Create()
|
158
|
+
{
|
159
|
+
return View();
|
160
|
+
}
|
161
|
+
|
162
|
+
// POST: hogeLists/Create
|
163
|
+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
164
|
+
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
165
|
+
[HttpPost]
|
166
|
+
[ValidateAntiForgeryToken]
|
167
|
+
public async Task<IActionResult> Create([Bind("Id,ReqFuncID,ReqFuncBigSep,ReqFuncMidSep,ReqFuncName")] hogeList hogeList)
|
168
|
+
{
|
169
|
+
if (ModelState.IsValid)
|
170
|
+
{
|
171
|
+
_context.Add(hogeList);
|
172
|
+
await _context.SaveChangesAsync();
|
173
|
+
return RedirectToAction(nameof(Index));
|
174
|
+
}
|
175
|
+
return View(hogeList);
|
176
|
+
}
|
177
|
+
|
178
|
+
// GET: hogeLists/Edit/5
|
179
|
+
public async Task<IActionResult> Edit(int? id)
|
180
|
+
{
|
181
|
+
if (id == null)
|
182
|
+
{
|
183
|
+
return NotFound();
|
184
|
+
}
|
185
|
+
|
186
|
+
var hogeList = await _context.hogeLists.FindAsync(id);
|
187
|
+
if (hogeList == null)
|
188
|
+
{
|
189
|
+
return NotFound();
|
190
|
+
}
|
191
|
+
return View(hogeList);
|
192
|
+
}
|
193
|
+
|
194
|
+
// POST: hogeLists/Edit/5
|
195
|
+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
196
|
+
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
197
|
+
[HttpPost]
|
198
|
+
[ValidateAntiForgeryToken]
|
199
|
+
public async Task<IActionResult> Edit(int id, [Bind("Id,ReqFuncID,ReqFuncBigSep,ReqFuncMidSep,ReqFuncName")] hogeList hogeList)
|
200
|
+
{
|
201
|
+
if (id != hogeList.Id)
|
202
|
+
{
|
203
|
+
return NotFound();
|
204
|
+
}
|
205
|
+
|
206
|
+
if (ModelState.IsValid)
|
207
|
+
{
|
208
|
+
try
|
209
|
+
{
|
210
|
+
_context.Update(hogeList);
|
211
|
+
await _context.SaveChangesAsync();
|
212
|
+
}
|
213
|
+
catch (DbUpdateConcurrencyException)
|
214
|
+
{
|
215
|
+
if (!hogeListExists(hogeList.Id))
|
216
|
+
{
|
217
|
+
return NotFound();
|
218
|
+
}
|
219
|
+
else
|
220
|
+
{
|
221
|
+
throw;
|
222
|
+
}
|
223
|
+
}
|
224
|
+
return RedirectToAction(nameof(Index));
|
225
|
+
}
|
226
|
+
return View(hogeList);
|
227
|
+
}
|
228
|
+
|
229
|
+
// GET: hogeLists/Delete/5
|
230
|
+
public async Task<IActionResult> Delete(int? id)
|
231
|
+
{
|
232
|
+
if (id == null)
|
233
|
+
{
|
234
|
+
return NotFound();
|
235
|
+
}
|
236
|
+
|
237
|
+
var hogeList = await _context.hogeLists
|
238
|
+
.FirstOrDefaultAsync(m => m.Id == id);
|
239
|
+
if (hogeList == null)
|
240
|
+
{
|
241
|
+
return NotFound();
|
242
|
+
}
|
243
|
+
|
244
|
+
return View(hogeList);
|
245
|
+
}
|
246
|
+
|
247
|
+
// POST: hogeLists/Delete/5
|
248
|
+
[HttpPost, ActionName("Delete")]
|
249
|
+
[ValidateAntiForgeryToken]
|
250
|
+
public async Task<IActionResult> DeleteConfirmed(int id)
|
251
|
+
{
|
252
|
+
var hogeList = await _context.hogeLists.FindAsync(id);
|
253
|
+
_context.hogeLists.Remove(hogeList);
|
254
|
+
await _context.SaveChangesAsync();
|
255
|
+
return RedirectToAction(nameof(Index));
|
256
|
+
}
|
257
|
+
|
258
|
+
private bool hogeListExists(int id)
|
259
|
+
{
|
260
|
+
return _context.hogeLists.Any(e => e.Id == id);
|
261
|
+
}
|
262
|
+
|
263
|
+
/////////////////////////////////////////////////
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
}
|
268
|
+
}
|
269
|
+
|
270
|
+
```
|
271
|
+
|
111
272
|
###エラー内容
|
112
273
|
今回の例だとHogeViewModelのページは正しく表示されますが、hogeListだけを表示するページでエラーが出ます。
|
113
274
|
hogeList単体だとViewModelの継承元というだけで直接は関わっていないと考えていますが、
|
114
275
|
SqlException: Invalid column name 'FuncMainViewModelId'.
|
115
276
|
と出て、ViewModelのIdについて言及されます。
|
116
|
-
しかしViewModelのIdを消すと
|
277
|
+
しかしViewModelのIdを消すと
|
278
|
+
InvalidOperationException: The entity type 'HogeViewModel' requires a primary key to be defined.
|
279
|
+
というエラーが出ます。
|
117
280
|
|
118
281
|
そもそもViewModelに主キーは必要なのですか?(参考URLのQiitaの記事だと主キーは書いていません。ASP.NET Coreじゃないからというのもあると思いますが・・・。)
|
119
282
|
|
2
文の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
こんにちは。
|
2
2
|
初質問なので拙いところがあるかと思いますがよろしくお願いします。
|
3
3
|
|
4
|
-
Windows10 ASP.NET Core 2.2において、元々ModelとViewが1対1対応でページを作成していたのですが、ViewModelを用いて複数のModelを1つのViewに表示出来る
|
4
|
+
Windows10 ASP.NET Core 2.2において、元々ModelとViewが1対1対応でページを作成していたのですが、ViewModelを用いて複数のModelを1つのViewに表示出来るページを作成すると、元となっている複数のModelのうちの1つのみを参照するページが表示出来なくなりました。
|
5
5
|
何故そうなったのか理由も含めて教えていただけると助かります。
|
6
6
|
言語はC#です。
|
7
7
|
### ソースコード
|
1
タグの追加, リンクの更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -123,9 +123,8 @@
|
|
123
123
|
|
124
124
|
### 参考URL
|
125
125
|
https://teratail.com/questions/74013
|
126
|
-
http://kuttsun.blogspot.com/2018/03/mvc-viewmodel.html
|
126
|
+
[http://kuttsun.blogspot.com/2018/03/mvc-viewmodel.html](http://kuttsun.blogspot.com/2018/03/mvc-viewmodel.html)
|
127
|
-
https://qiita.com/KktkiY/items/f28528916e97310262e0
|
127
|
+
[https://qiita.com/KktkiY/items/f28528916e97310262e0](https://qiita.com/KktkiY/items/f28528916e97310262e0)
|
128
|
-
|
129
128
|
### 補足情報(FW/ツールのバージョンなど)
|
130
129
|
Visual Studio 2017 Community
|
131
130
|
Microsoft.AspNetCore.All 2.2.8
|