- 在.cshtml文件中的表格标签中,加入“class”属性和“table-responsive”类,使表格具有响应式特性,能在较小的屏幕上自适应。
代码示例:
- 在控制器的.action方法中,使用EF Core对数据库进行查询,并将结果传递给视图。
代码示例:
public IActionResult Index()
{
var data = _context.MyTable.ToList(); // MyTable为你的表名
return View(data);
}
- 在.cshtml文件中,使用@foreach循环遍历数据,并将数据显示在表格中。
代码示例:
@foreach(var item in Model)
{
@item.Id |
@item.Name |
@item.Age |
@item.Gender |
}
其中,@Model为控制器传递给视图的数据。在此示例中,我们假设数据表中包含Id、Name、Age和Gender四列。在循环中,我们将每一行数据显示在表格的一行中。
相关内容