首页 > 解决方案 > 如何在 asp.net mvc 实体框架中创建视图模型、控制器和视图以加入多对多表

问题描述

我想加入并显示 tblOrder 、 tblCustomer 、 tblProduct 表中 OrderNo= givenId 的“ProductId、Quantity、UnitPrice、Discount & Total”列。

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; }
}

这是我创建的 ViewModel

public class OrderCustomerProductViewModel
{
    public OrderCustomerProductViewModel() { }
    public OrderCustomerProductViewModel(tblOrder orderow, tblCustomer customerow, tblProduct productrow)
    {
        OrderNo = orderow.OrderNo;
        CustomerName = customerow.CustomerName;
        ProductId = productrow.ProductId;
        Quantity = orderow.Quantity;
        UnitPrice = productrow.UnitPrice;
        Discount = orderow.Discount;
        Total = orderow.Total;
    }

    public int OrderNo { get; set; }
    public string CustomerName { get; set; }
    public int ProductId { get; set; }
    public Nullable<int> Quantity { get; set; }
    public string UnitPrice { get; set; }
    public Nullable<double> Discount { get; set; }
    public Nullable<double> Total { get; set; }
}

我尝试使用如下列表发送所有数据以查看,但遇到了一些错误。它说

ICollection 不包含 ProductId 和 UnitPrice 的定义

请帮我解决这个错误。提前致谢

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);
}

!我按照 [Jaggan_j] 所说的尝试过,但它不起作用。[Jaggan_j] 请帮帮我3

标签: asp.net-mvcentity-frameworkmany-to-manyviewmodeljointable

解决方案


ProductId 和 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);
}

然后在视图中,您只需遍历 tblProductList 以显示每个产品的 Id 和 Unit Price。


推荐阅读