Asp.net mvc 如何创建viewmodel、controller&;在asp.net mvc实体框架中连接多对多表的视图

Asp.net mvc 如何创建viewmodel、controller&;在asp.net mvc实体框架中连接多对多表的视图,asp.net-mvc,entity-framework,many-to-many,viewmodel,jointable,Asp.net Mvc,Entity Framework,Many To Many,Viewmodel,Jointable,我想加入并显示来自tblOrder、tblCustomer、TBLPProduct表的“ProductId、Quantity、UnitPrice、折扣和Total”列,其中OrderNo=givenId。 OrderNo通过文本框给出 订单表 public partial class tblOrder { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverri

我想加入并显示来自tblOrder、tblCustomer、TBLPProduct表的“ProductId、Quantity、UnitPrice、折扣和Total”列,其中OrderNo=givenId。

OrderNo通过文本框给出

订单表

public partial class tblOrder
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public tblOrder()
    {
        this.tblProducts = new HashSet<tblProduct>();
    }

    [Key]
    public int OrderNo { get; set; }
    public Nullable<int> Quantity { get; set; }
    public Nullable<double> Discount { get; set; }
    public Nullable<double> Total { get; set; }
    public Nullable<double> SubTotal { get; set; }
    public Nullable<double> DiscountTotal { get; set; }
    public Nullable<double> Tax { get; set; }
    public Nullable<double> NetTotal { get; set; }
    public int CustomerCode { get; set; }

    public virtual tblCustomer tblCustomer { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<tblProduct> tblProducts { get; set; }
}
public partial class tblProduct
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public tblProduct()
    {
        this.tblOrders = new HashSet<tblOrder>();
    }
    [Key]
    public int ProductId { get; set; }
    public string UnitPrice { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<tblOrder> tblOrders { get; set; }
}
public partial class tblCustomer
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public tblCustomer()
    {
        this.tblOrders = new HashSet<tblOrder>();
    }

    [Key]
    public int CustomerCode { get; set; }
    public string CustomerName { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<tblOrder> tblOrders { get; set; }
}
public ActionResult Index(int id)
{
    var results = (db.tblOrders.Where(l => l.OrderNo == id).Include(c => c.tblCustomer).Include(p => p.tblProducts)
          .Select(v => new OrderCustomerProductViewModel
              {                        
                  CustomerName = v.tblCustomer.CustomerName,
                  ProductId = v.tblProducts.ProductId,
                  Quantity = v.Quantity,
                  UnitPrice = v.tblProducts.UnitPrice,
                  Discount  = v.Discount,
                  Total = v.Total,
        })).ToList();

    return View(results);
}

产品ID和单价是单个产品的属性,而不是导致错误的产品集合的属性。因此,您必须通过返回产品列表来获取订单中的所有产品。可以将产品列表添加到视图模型中:

List<tblProduct> tblProductList { get; set; }

然后在视图中,您只需遍历tblProductList即可显示每个产品的Id和单价。

产品和订单表具有多对多关系,您可以使用链接表将它们连接起来。我不能使用链接表来连接多对多表,因为实体框架消除了链接表。谢谢您的回答。但我仍然在努力处理这段代码,上面我上传了一张遇到错误的图片。请检查一下并帮助我。这对我会有很大帮助。@Sameera Udakumbura-在第一个循环中使用另一个循环来迭代tblProductList,如
@foreach(item.tblProductList中的var-product){@Html.DisplayFor(model=>product.UnitPrice)}
List<tblProduct> tblProductList { get; set; }
public ActionResult Index(int id)
{
   var results = (db.tblOrders.Where(l => l.OrderNo == id).Include(c => c.tblCustomer)
                  .Include(p => p.tblProducts)
                  .Select(v => new OrderCustomerProductViewModel
                  {                        
                      tblProductList = v.tblProducts.ToList(),
                      --rest of your select--
                  })).ToList();

   return View(results);
}