如何在 ASP.NET MVC 中修复我的 table 关于 distinct 和 count 的问题
how to I fix my table about distinct and count in ASP.NET MVC
我有关于我的 table 和我的 Count 和 Distinct
的问题
这是我的 RazorView
<table id="example2" class="table table-bordered table-striped">
<thead>
<tr>
<th rowspan="2">Tahun</th>
<th colspan="2">Jml. F1</th>
<th rowspan="2">Jml. F2 PR</th>
<th rowspan="2">Jml. F2 PE</th>
</tr>
<tr>
<th>Sementara</th>
<th>Tetap</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.rol_tb_form1List.Select(x => x.tgl_moc.Year).Distinct())
{
<tr>
<td>@Html.DisplayFor(x => item)</td>
</tr>
}
@foreach (var form1 in Model.rol_tb_form1List.Where(y => y.jenis_perubahan == "sementara").GroupBy(x => x.tgl_moc.Year))
{
<tr>
<td>@Html.DisplayFor(x => form1).ToString().Count()</td>
</tr>
}
@foreach (var form1 in Model.rol_tb_form1List.Where(y => y.jenis_perubahan == "tetap").GroupBy(x => x.tgl_moc.Year))
{
<tr>
<td>@Html.DisplayFor(x => form1).ToString().Count()</td>
</tr>
}
</tbody>
</table>
有人可以修复我的 table 或我的 Count 或我的 Distinct 代码吗..
抱歉我的英语不好
这是我的控制器
public ActionResult Rekap()
{
Setting setting = new Setting();
setting.rol_tb_form1List = db.rol_tb_form1.ToList();
return View(setting);
}
我只是用一个简单的控制器,但我不知道这个对不对..
该代码的结果
我想要 Sementara 列中的值 19 18 42 49
和 Tetap 列中的值 377 363 63 13
这是我的示例数据,这只是示例
我想要那个例子的结果是这样的
您应该始终遵循的一些原则:
- 您的控制器负责将正确的数据传递给
查看(您的视图不应包含查询)
- 创建视图模型来表示您想要的表示方式
视图中的数据
在这种情况下,您需要一个视图模型来表示您的 table,并表示 table
中的每一行
public class RowVM
{
// Properties for each column
public int Tahun { get; set; }
public int Sementara { get; set; }
public int Tetap { get; set; }
}
public class TableVM
{
public Enumerable<RowVM> Rows { get; set; }
.... // other properties as required to represent the table header, footer etc
}
然后在 GET 方法中,将查询更改为 return 数据并将其投影到您的视图模型中
var data = db.rol_tb_form1.AsEnumerable().GroupBy(x => x.tgl_moc.Year).Select(x => new RowVM
{
Tahun = x.Key,
Sementara = x.Where(y => y.genis_perubahan == "sementara").Count(),
Tetap = x.Where(y => y.genis_perubahan == "tetap").Count()
});
TableVM model = new TableVM
{
Rows = data,
....
};
return View(model);
而且视图很简单
@model TableVM
....
<table>
<thead> .... </thead>
<tbody>
@foreach (var row in Model.Rows)
{
<tr>
<td>@row.Tahun<td>
<td>@row.Sementara<td>
<td>@row.Tetap<td>
</tr>
}
</tbody>
</table>
请注意,我省略了 table 标题,因为不清楚您要对第一张图片做什么,但由于行包含 3 列,因此您的标题也应包含 3 列。
我有关于我的 table 和我的 Count 和 Distinct
的问题这是我的 RazorView
<table id="example2" class="table table-bordered table-striped">
<thead>
<tr>
<th rowspan="2">Tahun</th>
<th colspan="2">Jml. F1</th>
<th rowspan="2">Jml. F2 PR</th>
<th rowspan="2">Jml. F2 PE</th>
</tr>
<tr>
<th>Sementara</th>
<th>Tetap</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.rol_tb_form1List.Select(x => x.tgl_moc.Year).Distinct())
{
<tr>
<td>@Html.DisplayFor(x => item)</td>
</tr>
}
@foreach (var form1 in Model.rol_tb_form1List.Where(y => y.jenis_perubahan == "sementara").GroupBy(x => x.tgl_moc.Year))
{
<tr>
<td>@Html.DisplayFor(x => form1).ToString().Count()</td>
</tr>
}
@foreach (var form1 in Model.rol_tb_form1List.Where(y => y.jenis_perubahan == "tetap").GroupBy(x => x.tgl_moc.Year))
{
<tr>
<td>@Html.DisplayFor(x => form1).ToString().Count()</td>
</tr>
}
</tbody>
</table>
有人可以修复我的 table 或我的 Count 或我的 Distinct 代码吗..
抱歉我的英语不好
这是我的控制器
public ActionResult Rekap()
{
Setting setting = new Setting();
setting.rol_tb_form1List = db.rol_tb_form1.ToList();
return View(setting);
}
我只是用一个简单的控制器,但我不知道这个对不对..
该代码的结果
我想要 Sementara 列中的值 19 18 42 49
和 Tetap 列中的值 377 363 63 13
这是我的示例数据,这只是示例
我想要那个例子的结果是这样的
您应该始终遵循的一些原则:
- 您的控制器负责将正确的数据传递给 查看(您的视图不应包含查询)
- 创建视图模型来表示您想要的表示方式 视图中的数据
在这种情况下,您需要一个视图模型来表示您的 table,并表示 table
中的每一行public class RowVM
{
// Properties for each column
public int Tahun { get; set; }
public int Sementara { get; set; }
public int Tetap { get; set; }
}
public class TableVM
{
public Enumerable<RowVM> Rows { get; set; }
.... // other properties as required to represent the table header, footer etc
}
然后在 GET 方法中,将查询更改为 return 数据并将其投影到您的视图模型中
var data = db.rol_tb_form1.AsEnumerable().GroupBy(x => x.tgl_moc.Year).Select(x => new RowVM
{
Tahun = x.Key,
Sementara = x.Where(y => y.genis_perubahan == "sementara").Count(),
Tetap = x.Where(y => y.genis_perubahan == "tetap").Count()
});
TableVM model = new TableVM
{
Rows = data,
....
};
return View(model);
而且视图很简单
@model TableVM
....
<table>
<thead> .... </thead>
<tbody>
@foreach (var row in Model.Rows)
{
<tr>
<td>@row.Tahun<td>
<td>@row.Sementara<td>
<td>@row.Tetap<td>
</tr>
}
</tbody>
</table>
请注意,我省略了 table 标题,因为不清楚您要对第一张图片做什么,但由于行包含 3 列,因此您的标题也应包含 3 列。