我的代码第一类中有 1:m 关系。我的视图的订单实例显示具有唯一 OrderID 的值。由于一个客户可以有多个订单,因此外键 CustID 在此视图中不是唯一的。
public class Customers
{
public int CustomerID { get; set; }
[StringLength(50, ErrorMessage = "{0} cannot exceed {1} characters")]
public string LastName { get; set; }
public string Initials { get; set; }
public string email {get; set;}
public List<Orders> orders { get; set; }
}
订单类:
public class Orders
{
[Key]
public int OrdersID { get; set; }
public int OrderName { get; set; }
public int? CustomerID { get; set; }
public virtual Customers customer { get; set; }
}
下面是我的控制器:
private custContext db = new custContext();
//
// GET: /cust/
public ViewResult Index()
{
var cust = db.orders.Include(c => c.customer).Take(10);
return View(cust.ToList());
}
如何在我的视图中仅显示不同的客户,而他们的OrderNames在同一视图中?