回答編集履歴
5
追記
answer
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Entity Frameworkを使用してデータベースからEntityのコレクションを取得できるように環境を整える必要があります。
|
7
7
|
|
8
|
-
※接続するプロバイダーによってはGroupByがSQL等の命令に変換できず、[Client side GroupBy is not supported](https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported) 等のエラーが出るため、その場合はEntityのコレクションを取得し、ToListメソッド等でIEnumerableに変換した上でLINQ to ObjectsのGroupByを使ってください。
|
8
|
+
※接続するプロバイダーによってはGroupByがSQL等の命令に変換できず、[Client side GroupBy is not supported](https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported) 等のエラーが出るため、その場合はEntityのコレクションを取得し、ToListメソッド等でIEnumerableに変換した上でLINQ to EntitiesではなくLINQ to ObjectsのGroupByを使ってください。
|
9
9
|
|
10
10
|
> しかしそれを実装するにしてもその処理をどこに記述するべきなのか(Controller? View?)わかっていません。
|
11
11
|
> View側(cshtmlかjavascript)でデータの編集をしても良いのでしょうか?
|
4
なぜか修正が反映されていない
answer
CHANGED
File without changes
|
3
なぜか修正が反映されていない
answer
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Entity Frameworkを使用してデータベースからEntityのコレクションを取得できるように環境を整える必要があります。
|
7
7
|
|
8
|
-
※接続するプロバイダーによってはGroupByがSQL等の命令に変換できず、[Client side GroupBy is not supported](https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported) 等のエラーが出るため、その場合はEntityのコレクションを取得し、IEnumerableに変換した上でLINQ to ObjectsのGroupByを使ってください。
|
8
|
+
※接続するプロバイダーによってはGroupByがSQL等の命令に変換できず、[Client side GroupBy is not supported](https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported) 等のエラーが出るため、その場合はEntityのコレクションを取得し、ToListメソッド等でIEnumerableに変換した上でLINQ to ObjectsのGroupByを使ってください。
|
9
9
|
|
10
10
|
> しかしそれを実装するにしてもその処理をどこに記述するべきなのか(Controller? View?)わかっていません。
|
11
11
|
> View側(cshtmlかjavascript)でデータの編集をしても良いのでしょうか?
|
2
エラーが出る件を追記(要コメント欄確認)
answer
CHANGED
@@ -2,8 +2,11 @@
|
|
2
2
|
今のところ、元のModelからデータを全て取ってきてForなりで識別子の種類数を算出し、その分ViewでTableを作成して割り振るといった処理を考えています。
|
3
3
|
|
4
4
|
LINQ to Entitiesの [Queryable.GroupBy メソッド](https://docs.microsoft.com/ja-jp/dotnet/api/system.linq.queryable.groupby?view=net-5.0)を使ってください。
|
5
|
+
|
5
6
|
Entity Frameworkを使用してデータベースからEntityのコレクションを取得できるように環境を整える必要があります。
|
6
7
|
|
8
|
+
※接続するプロバイダーによってはGroupByがSQL等の命令に変換できず、[Client side GroupBy is not supported](https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported) 等のエラーが出るため、その場合はEntityのコレクションを取得し、IEnumerableに変換した上でLINQ to ObjectsのGroupByを使ってください。
|
9
|
+
|
7
10
|
> しかしそれを実装するにしてもその処理をどこに記述するべきなのか(Controller? View?)わかっていません。
|
8
11
|
> View側(cshtmlかjavascript)でデータの編集をしても良いのでしょうか?
|
9
12
|
それともModelに関することはController側で処理するのが好ましいですか?
|
1
ソースコード追加
answer
CHANGED
@@ -21,4 +21,213 @@
|
|
21
21
|
|
22
22
|
View
|
23
23
|
- Razor構文を使用してforeachで各グループ毎にtableタグを使い表を作る
|
24
|
-
- それぞれのtableタグでグループが持つ各要素毎にtr,thタグなどで行を追加&行にデータを書き込む
|
24
|
+
- それぞれのtableタグでグループが持つ各要素毎にtr,thタグなどで行を追加&行にデータを書き込む
|
25
|
+
|
26
|
+
# Sample
|
27
|
+
|
28
|
+
EFからスキャフォールディングで生成したControllerをベースに、Indexメソッドだけ弄ってます。
|
29
|
+
|
30
|
+
```C#
|
31
|
+
using System;
|
32
|
+
using System.Collections.Generic;
|
33
|
+
using System.Linq;
|
34
|
+
using System.Threading.Tasks;
|
35
|
+
using Microsoft.AspNetCore.Mvc;
|
36
|
+
using Microsoft.AspNetCore.Mvc.Rendering;
|
37
|
+
using Microsoft.EntityFrameworkCore;
|
38
|
+
using WebApplication3.Models;
|
39
|
+
|
40
|
+
namespace WebApplication3.Controllers
|
41
|
+
{
|
42
|
+
public class SampleController : Controller
|
43
|
+
{
|
44
|
+
private readonly SampleContext _context;
|
45
|
+
|
46
|
+
public SampleController(SampleContext context)
|
47
|
+
{
|
48
|
+
_context = context;
|
49
|
+
}
|
50
|
+
|
51
|
+
// GET: Sample
|
52
|
+
public async Task<IActionResult> Index()
|
53
|
+
{
|
54
|
+
return View(await _context.SampleDataModels.GroupBy(x => x.Identifier).ToListAsync());
|
55
|
+
}
|
56
|
+
|
57
|
+
// GET: Sample/Details/5
|
58
|
+
public async Task<IActionResult> Details(int? id)
|
59
|
+
{
|
60
|
+
if (id == null)
|
61
|
+
{
|
62
|
+
return NotFound();
|
63
|
+
}
|
64
|
+
|
65
|
+
var sampleDataModel = await _context.SampleDataModels
|
66
|
+
.FirstOrDefaultAsync(m => m.Id == id);
|
67
|
+
if (sampleDataModel == null)
|
68
|
+
{
|
69
|
+
return NotFound();
|
70
|
+
}
|
71
|
+
|
72
|
+
return View(sampleDataModel);
|
73
|
+
}
|
74
|
+
|
75
|
+
// GET: Sample/Create
|
76
|
+
public IActionResult Create()
|
77
|
+
{
|
78
|
+
return View();
|
79
|
+
}
|
80
|
+
|
81
|
+
// POST: Sample/Create
|
82
|
+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
83
|
+
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
84
|
+
[HttpPost]
|
85
|
+
[ValidateAntiForgeryToken]
|
86
|
+
public async Task<IActionResult> Create([Bind("Id,Identifier,Name")] SampleDataModel sampleDataModel)
|
87
|
+
{
|
88
|
+
if (ModelState.IsValid)
|
89
|
+
{
|
90
|
+
_context.Add(sampleDataModel);
|
91
|
+
await _context.SaveChangesAsync();
|
92
|
+
return RedirectToAction(nameof(Index));
|
93
|
+
}
|
94
|
+
return View(sampleDataModel);
|
95
|
+
}
|
96
|
+
|
97
|
+
// GET: Sample/Edit/5
|
98
|
+
public async Task<IActionResult> Edit(int? id)
|
99
|
+
{
|
100
|
+
if (id == null)
|
101
|
+
{
|
102
|
+
return NotFound();
|
103
|
+
}
|
104
|
+
|
105
|
+
var sampleDataModel = await _context.SampleDataModels.FindAsync(id);
|
106
|
+
if (sampleDataModel == null)
|
107
|
+
{
|
108
|
+
return NotFound();
|
109
|
+
}
|
110
|
+
return View(sampleDataModel);
|
111
|
+
}
|
112
|
+
|
113
|
+
// POST: Sample/Edit/5
|
114
|
+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
115
|
+
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
116
|
+
[HttpPost]
|
117
|
+
[ValidateAntiForgeryToken]
|
118
|
+
public async Task<IActionResult> Edit(int id, [Bind("Id,Identifier,Name")] SampleDataModel sampleDataModel)
|
119
|
+
{
|
120
|
+
if (id != sampleDataModel.Id)
|
121
|
+
{
|
122
|
+
return NotFound();
|
123
|
+
}
|
124
|
+
|
125
|
+
if (ModelState.IsValid)
|
126
|
+
{
|
127
|
+
try
|
128
|
+
{
|
129
|
+
_context.Update(sampleDataModel);
|
130
|
+
await _context.SaveChangesAsync();
|
131
|
+
}
|
132
|
+
catch (DbUpdateConcurrencyException)
|
133
|
+
{
|
134
|
+
if (!SampleDataModelExists(sampleDataModel.Id))
|
135
|
+
{
|
136
|
+
return NotFound();
|
137
|
+
}
|
138
|
+
else
|
139
|
+
{
|
140
|
+
throw;
|
141
|
+
}
|
142
|
+
}
|
143
|
+
return RedirectToAction(nameof(Index));
|
144
|
+
}
|
145
|
+
return View(sampleDataModel);
|
146
|
+
}
|
147
|
+
|
148
|
+
// GET: Sample/Delete/5
|
149
|
+
public async Task<IActionResult> Delete(int? id)
|
150
|
+
{
|
151
|
+
if (id == null)
|
152
|
+
{
|
153
|
+
return NotFound();
|
154
|
+
}
|
155
|
+
|
156
|
+
var sampleDataModel = await _context.SampleDataModels
|
157
|
+
.FirstOrDefaultAsync(m => m.Id == id);
|
158
|
+
if (sampleDataModel == null)
|
159
|
+
{
|
160
|
+
return NotFound();
|
161
|
+
}
|
162
|
+
|
163
|
+
return View(sampleDataModel);
|
164
|
+
}
|
165
|
+
|
166
|
+
// POST: Sample/Delete/5
|
167
|
+
[HttpPost, ActionName("Delete")]
|
168
|
+
[ValidateAntiForgeryToken]
|
169
|
+
public async Task<IActionResult> DeleteConfirmed(int id)
|
170
|
+
{
|
171
|
+
var sampleDataModel = await _context.SampleDataModels.FindAsync(id);
|
172
|
+
_context.SampleDataModels.Remove(sampleDataModel);
|
173
|
+
await _context.SaveChangesAsync();
|
174
|
+
return RedirectToAction(nameof(Index));
|
175
|
+
}
|
176
|
+
|
177
|
+
private bool SampleDataModelExists(int id)
|
178
|
+
{
|
179
|
+
return _context.SampleDataModels.Any(e => e.Id == id);
|
180
|
+
}
|
181
|
+
}
|
182
|
+
}
|
183
|
+
```
|
184
|
+
|
185
|
+
```cshtml
|
186
|
+
@model IEnumerable<IGrouping<string,WebApplication3.Models.SampleDataModel>>
|
187
|
+
|
188
|
+
@{
|
189
|
+
ViewData["Title"] = "Index";
|
190
|
+
}
|
191
|
+
|
192
|
+
<h2>Index</h2>
|
193
|
+
|
194
|
+
<p>
|
195
|
+
<a asp-action="Create">Create New</a>
|
196
|
+
</p>
|
197
|
+
|
198
|
+
@foreach (var group in Model)
|
199
|
+
{
|
200
|
+
<table class="table">
|
201
|
+
<thead>
|
202
|
+
<tr>
|
203
|
+
<th>
|
204
|
+
@Html.DisplayNameFor(model => model.First().Identifier)
|
205
|
+
</th>
|
206
|
+
<th>
|
207
|
+
@Html.DisplayNameFor(model => model.First().Name)
|
208
|
+
</th>
|
209
|
+
<th></th>
|
210
|
+
</tr>
|
211
|
+
</thead>
|
212
|
+
<tbody>
|
213
|
+
@foreach (var item in group)
|
214
|
+
{
|
215
|
+
<tr>
|
216
|
+
<td>
|
217
|
+
@Html.DisplayFor(modelItem => item.Identifier)
|
218
|
+
</td>
|
219
|
+
<td>
|
220
|
+
@Html.DisplayFor(modelItem => item.Name)
|
221
|
+
</td>
|
222
|
+
<td>
|
223
|
+
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
|
224
|
+
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
|
225
|
+
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
|
226
|
+
</td>
|
227
|
+
</tr>
|
228
|
+
}
|
229
|
+
</tbody>
|
230
|
+
</table>
|
231
|
+
}
|
232
|
+
```
|
233
|
+

|