【问题标题】:How to create viewmodel ,controller & view to join many to many table in asp.net mvc Entity framework如何创建视图模型、控制器和视图以在 asp.net mvc 实体框架中加入多对多表
【发布时间】:2019-06-19 13:01:02
【问题描述】:

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

OrderNo 通过文本框给出。This is how it should be

DataBase

订单表

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

! I tried as [Jaggan_j] said but it doesn't work. [Jaggan_j] Please help me3

【问题讨论】:

  • Product 和 Order 表是多对多的关系,您可以通过链接表将它们连接起来
  • 我不能使用链接表来连接多对多表,因为实体框架消除了链接表。

标签: asp.net-mvc entity-framework many-to-many viewmodel jointable


【解决方案1】:

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

然后在 View 中,您只需遍历 tblProductList 即可显示每个产品的 Id 和 Unit Price。

【讨论】:

  • 感谢您的回答。但我仍然在为这段代码苦苦挣扎,上面我上传了一张遇到错误的图片。请检查并帮助我。这对我有很大的帮助。
  • @Sameera Udakumbura - 在您的第一个循环中使用另一个循环来遍历 tblProductList,例如 @foreach(var product in item.tblProductList) { @Html.DisplayFor(model =&gt; product.UnitPrice) }
猜你喜欢
  • 2014-11-30
  • 1970-01-01
  • 2011-06-06
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2012-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多