質問編集履歴

4

文の修正

2020/11/24 08:29

投稿

K-actus
K-actus

スコア22

test CHANGED
File without changes
test CHANGED
@@ -330,7 +330,7 @@
330
330
 
331
331
  [ValidateAntiForgeryToken]
332
332
 
333
- public async Task<IActionResult> Create([Bind("Id,ReqFuncID,ReqFuncBigSep,ReqFuncMidSep,ReqFuncName")] hogeList hogeList)
333
+ public async Task<IActionResult> Create([Bind("Id, hoge")] hogeList hogeList)
334
334
 
335
335
  {
336
336
 
@@ -394,7 +394,7 @@
394
394
 
395
395
  [ValidateAntiForgeryToken]
396
396
 
397
- public async Task<IActionResult> Edit(int id, [Bind("Id,ReqFuncID,ReqFuncBigSep,ReqFuncMidSep,ReqFuncName")] hogeList hogeList)
397
+ public async Task<IActionResult> Edit(int id, [Bind("Id, hoge")] hogeList hogeList)
398
398
 
399
399
  {
400
400
 

3

ソースコード、エラーの追加

2020/11/24 08:28

投稿

K-actus
K-actus

スコア22

test CHANGED
File without changes
test CHANGED
@@ -216,6 +216,328 @@
216
216
 
217
217
  hogeListのView及びControllerはScaffoldingで作成しました。
218
218
 
219
+ ```Controller
220
+
221
+ using System;
222
+
223
+ using System.Collections.Generic;
224
+
225
+ using System.Linq;
226
+
227
+ using System.Threading.Tasks;
228
+
229
+ using Microsoft.AspNetCore.Mvc;
230
+
231
+ using Microsoft.AspNetCore.Mvc.Rendering;
232
+
233
+ using Microsoft.EntityFrameworkCore;
234
+
235
+ using DBAccessSample.Data;
236
+
237
+ using DBAccessSample.Models;
238
+
239
+
240
+
241
+ namespace DBAccessSample.Controllers
242
+
243
+ {
244
+
245
+ public class hogeListsController : Controller
246
+
247
+ {
248
+
249
+ private readonly MyDbContext _context;
250
+
251
+
252
+
253
+ public hogeListsController(MyDbContext context)
254
+
255
+ {
256
+
257
+ _context = context;
258
+
259
+ }
260
+
261
+
262
+
263
+ // GET: hogeLists
264
+
265
+ public async Task<IActionResult> Index()
266
+
267
+ {
268
+
269
+ return View(await _context.hogeLists.ToListAsync());
270
+
271
+ }
272
+
273
+
274
+
275
+ // GET: hogeLists/Details/5
276
+
277
+ public async Task<IActionResult> Details(int? id)
278
+
279
+ {
280
+
281
+ if (id == null)
282
+
283
+ {
284
+
285
+ return NotFound();
286
+
287
+ }
288
+
289
+
290
+
291
+ var hogeList = await _context.hogeLists
292
+
293
+ .FirstOrDefaultAsync(m => m.Id == id);
294
+
295
+ if (hogeList == null)
296
+
297
+ {
298
+
299
+ return NotFound();
300
+
301
+ }
302
+
303
+
304
+
305
+ return View(hogeList);
306
+
307
+ }
308
+
309
+
310
+
311
+ // GET: hogeLists/Create
312
+
313
+ public IActionResult Create()
314
+
315
+ {
316
+
317
+ return View();
318
+
319
+ }
320
+
321
+
322
+
323
+ // POST: hogeLists/Create
324
+
325
+ // To protect from overposting attacks, please enable the specific properties you want to bind to, for
326
+
327
+ // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
328
+
329
+ [HttpPost]
330
+
331
+ [ValidateAntiForgeryToken]
332
+
333
+ public async Task<IActionResult> Create([Bind("Id,ReqFuncID,ReqFuncBigSep,ReqFuncMidSep,ReqFuncName")] hogeList hogeList)
334
+
335
+ {
336
+
337
+ if (ModelState.IsValid)
338
+
339
+ {
340
+
341
+ _context.Add(hogeList);
342
+
343
+ await _context.SaveChangesAsync();
344
+
345
+ return RedirectToAction(nameof(Index));
346
+
347
+ }
348
+
349
+ return View(hogeList);
350
+
351
+ }
352
+
353
+
354
+
355
+ // GET: hogeLists/Edit/5
356
+
357
+ public async Task<IActionResult> Edit(int? id)
358
+
359
+ {
360
+
361
+ if (id == null)
362
+
363
+ {
364
+
365
+ return NotFound();
366
+
367
+ }
368
+
369
+
370
+
371
+ var hogeList = await _context.hogeLists.FindAsync(id);
372
+
373
+ if (hogeList == null)
374
+
375
+ {
376
+
377
+ return NotFound();
378
+
379
+ }
380
+
381
+ return View(hogeList);
382
+
383
+ }
384
+
385
+
386
+
387
+ // POST: hogeLists/Edit/5
388
+
389
+ // To protect from overposting attacks, please enable the specific properties you want to bind to, for
390
+
391
+ // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
392
+
393
+ [HttpPost]
394
+
395
+ [ValidateAntiForgeryToken]
396
+
397
+ public async Task<IActionResult> Edit(int id, [Bind("Id,ReqFuncID,ReqFuncBigSep,ReqFuncMidSep,ReqFuncName")] hogeList hogeList)
398
+
399
+ {
400
+
401
+ if (id != hogeList.Id)
402
+
403
+ {
404
+
405
+ return NotFound();
406
+
407
+ }
408
+
409
+
410
+
411
+ if (ModelState.IsValid)
412
+
413
+ {
414
+
415
+ try
416
+
417
+ {
418
+
419
+ _context.Update(hogeList);
420
+
421
+ await _context.SaveChangesAsync();
422
+
423
+ }
424
+
425
+ catch (DbUpdateConcurrencyException)
426
+
427
+ {
428
+
429
+ if (!hogeListExists(hogeList.Id))
430
+
431
+ {
432
+
433
+ return NotFound();
434
+
435
+ }
436
+
437
+ else
438
+
439
+ {
440
+
441
+ throw;
442
+
443
+ }
444
+
445
+ }
446
+
447
+ return RedirectToAction(nameof(Index));
448
+
449
+ }
450
+
451
+ return View(hogeList);
452
+
453
+ }
454
+
455
+
456
+
457
+ // GET: hogeLists/Delete/5
458
+
459
+ public async Task<IActionResult> Delete(int? id)
460
+
461
+ {
462
+
463
+ if (id == null)
464
+
465
+ {
466
+
467
+ return NotFound();
468
+
469
+ }
470
+
471
+
472
+
473
+ var hogeList = await _context.hogeLists
474
+
475
+ .FirstOrDefaultAsync(m => m.Id == id);
476
+
477
+ if (hogeList == null)
478
+
479
+ {
480
+
481
+ return NotFound();
482
+
483
+ }
484
+
485
+
486
+
487
+ return View(hogeList);
488
+
489
+ }
490
+
491
+
492
+
493
+ // POST: hogeLists/Delete/5
494
+
495
+ [HttpPost, ActionName("Delete")]
496
+
497
+ [ValidateAntiForgeryToken]
498
+
499
+ public async Task<IActionResult> DeleteConfirmed(int id)
500
+
501
+ {
502
+
503
+ var hogeList = await _context.hogeLists.FindAsync(id);
504
+
505
+ _context.hogeLists.Remove(hogeList);
506
+
507
+ await _context.SaveChangesAsync();
508
+
509
+ return RedirectToAction(nameof(Index));
510
+
511
+ }
512
+
513
+
514
+
515
+ private bool hogeListExists(int id)
516
+
517
+ {
518
+
519
+ return _context.hogeLists.Any(e => e.Id == id);
520
+
521
+ }
522
+
523
+
524
+
525
+ /////////////////////////////////////////////////
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+ }
534
+
535
+ }
536
+
537
+
538
+
539
+ ```
540
+
219
541
 
220
542
 
221
543
  ###エラー内容
@@ -228,7 +550,11 @@
228
550
 
229
551
  と出て、ViewModelのIdについて言及されます。
230
552
 
231
- しかしViewModelのIdを消すとViewModelにprimary key がないというエラーが出ます。
553
+ しかしViewModelのIdを消すと
554
+
555
+ InvalidOperationException: The entity type 'HogeViewModel' requires a primary key to be defined.
556
+
557
+ というエラーが出ます。
232
558
 
233
559
 
234
560
 

2

文の修正

2020/11/24 08:26

投稿

K-actus
K-actus

スコア22

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- Windows10 ASP.NET Core 2.2において、元々ModelとViewが1対1対応でページを作成していたのですが、ViewModelを用いて複数のModelを1つのViewに表示出来るようにすると、元となっている複数のModelのうちの1つのみを参照するページが表示出来なくなりました。
7
+ Windows10 ASP.NET Core 2.2において、元々ModelとViewが1対1対応でページを作成していたのですが、ViewModelを用いて複数のModelを1つのViewに表示出来るページを作成すると、元となっている複数のModelのうちの1つのみを参照するページが表示出来なくなりました。
8
8
 
9
9
  何故そうなったのか理由も含めて教えていただけると助かります。
10
10
 

1

タグの追加, リンクの更新

2020/11/24 08:12

投稿

K-actus
K-actus

スコア22

test CHANGED
File without changes
test CHANGED
@@ -248,11 +248,9 @@
248
248
 
249
249
  https://teratail.com/questions/74013
250
250
 
251
- http://kuttsun.blogspot.com/2018/03/mvc-viewmodel.html
251
+ [http://kuttsun.blogspot.com/2018/03/mvc-viewmodel.html](http://kuttsun.blogspot.com/2018/03/mvc-viewmodel.html)
252
-
252
+
253
- https://qiita.com/KktkiY/items/f28528916e97310262e0
253
+ [https://qiita.com/KktkiY/items/f28528916e97310262e0](https://qiita.com/KktkiY/items/f28528916e97310262e0)
254
-
255
-
256
254
 
257
255
  ### 補足情報(FW/ツールのバージョンなど)
258
256