我在看书 C# 8.0and .NET Core 3.0 - Modern Cross-Platform Development
我目前正在学习第 15 章的练习 15.2,我们的任务是创建一个 Razor 页面,该页面会生成按国家/地区分组的客户列表。当您点击客户名称时,它会将您带到一个新页面,其中显示该客户的完整联系方式及其订单列表。
接下来,我构建了一个页面,其中包含一张卡片,标题为国家/地区,然后在卡片上列出每个客户的姓名。但是,我的 foreach 循环会吐出 Customer.Country 下的每个数据列。因此,如果有 11 个国家有德国,它会制作 11 张以德国为标题的卡片 ( see image )。
它还会填充每个国家/地区下的所有客户名称。
我发现我可以使用 GroupBy() 但正如我在下面解释的那样,这会导致无效的转换异常。
customers.cshtml
@page
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using NorthwindEntitiesLib
@model NorthwindWeb.Pages.CustomersModel
<div class="row">
<h1 class="display2">Customers</h1>
</div>
<div class="row">
@foreach (Customer customer in Model.Customers)
{
<div class="card border-info mb-3" style="max-width: 18rem">
<div class="card-header text-white bg-info">
@customer.Country
</div>
<ul class="list-group list-group-flush">
@foreach (var name in Model.Customers)
{
<li class="list-group-item">@name.ContactName</li>
}
</ul>
</div>
}
</div>
customers.cshtml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using NorthwindContextLib;
using NorthwindEntitiesLib;
namespace NorthwindWeb.Pages
{
public class CustomersModel : PageModel
{
private Northwind db;
public CustomersModel(Northwind injectedContext)
{
db = injectedContext;
}
public IEnumerable<Customer> Customers { get; set; }
public void OnGet()
{
Customers = db.Customers
.ToArray();
}
}
}
NorthwindEntitesLib
namespace NorthwindEntitiesLib
{
public class Customer
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public ICollection<Order> Orders { get; set; }
}
}
我曾尝试使用 GroupBy 对国家/地区进行分组,然后填充列表,但它给出了无效字符串类型转换的错误。
@foreach (Customer customer in Model.Customers.GroupBy(c =>c.Country))
{
<div class="card border-info mb-3" style="max-width: 18rem">
<div class="card-header text-white bg-info">
@customer.Country
</div>
<ul class="list-group list-group-flush">
@foreach (var name in Model.Customers)
{
<li class="list-group-item">@name.ContactName</li>
}
</ul>
</div>
}
InvalidCastException:无法将“System.Linq.Grouping`2[System.String,NorthwindEntitiesLib.Customer]”类型的对象转换为类型“NorthwindEntitiesLib.Customer”。
最佳答案
好的,GroupBy
为您提供了一个 Key
属性。这就是您将用于国家/地区名称的内容。
所以基本上,您将使用以下内容(警告未经测试)
@foreach (var country in Model.Customers.GroupBy(c =>c.Country))
{
<div class="card border-info mb-3" style="max-width: 18rem">
<div class="card-header text-white bg-info">
@country.Key
</div>
<ul class="list-group list-group-flush">
@foreach (var customer in country.ToArray())
{
<li class="list-group-item">@customer.ContactName</li>
}
</ul>
</div>
}
关于c# - 如何在 .NET Core Razor 页面中使用 foreach 循环对表数据进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61642925/