asp.net-mvc - 如何在 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);
}
解决方案
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。
推荐阅读
- google-chrome - 谷歌浏览器扩展:显示 ssr 或 csr
- nativescript - Nativescript-vue 简单的选择组件,如 web
- python - 如何获得重复运动的相应名称和权重组......?
- ios - 如何过滤数组以更新 tableview?
- c - 为什么这段代码会给我分段错误?
- excel - 如何在 VBA 中选择和复制 excel 的不同部分到 excel 中?
- c# - Visual Studio 扩展:如何禁用本机命令?
- c++ - 如何在directx11着色器中使用swizzle .rrrg
- r - 根据模式从字符串中提取项目
- ios - 有什么方法可以在 iOS 中归档发布版本之前自动运行我的所有单元测试