如何在 asp.net mvc 中的同一个 foreach 中显示多个模型?

How to display multiple models within the same foreach in asp.net mvc?

当存在多对多时,如何在同一个 foreach 中显示多个模型 table?

有 3 个 table (tblProduct, tblCustomer, tblOrder)。 tblOrder & tblProduct 是多对多,而 tblCustomer & tblOrder 是一对多。

我想加入这3个table并显示与OrderNo相关的记录。

我已经使用 Entity Framework 数据库优先(所以我没有中间 table 用于多对多关系)。

请帮帮我。我是 MVC 的新手。提前致谢。

这些是我的 tables:

Product table:

public partial class tblProduct
{
    [Key]
    public int ProductId { get; set; }
    public string UnitPrice { get; set; }

    public virtual ICollection<tblOrder> tblOrders { get; set; }
}

Customer table:

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

    public virtual ICollection<tblOrder> tblOrders { get; set; }
}

Order table:

public partial class 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; }
    public virtual ICollection<tblProduct> tblProducts { get; set; }
}

查看我创建的模型:

public class OrderCustomerProductViewModel
{
    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; }
}

我试过了,但出现错误:

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
          {
              OrderNo  = v.OrderNo,
              CustomerName= v.tblCustomer.CustomerName,
              ProductId = v.tblProducts.ProductId, <------ ERROR
              Quantity = v.Quantity,
              UnitPrice = v.tblProducts.UnitPrice,  <------ ERROR
              Discount = v.Discount,
              Total = v.Total,
          })).ToList();

    return View(results);
}

这是我的 View.cshtml:

@foreach (var item in Model)
{
    <tr id="rowtest">
        <td height="100">@Html.DisplayFor(model => item.OrderNo)</td>
        <td height="100">@Html.DisplayFor(model => item.CustomerName)</td>
        <td height="100">@Html.DisplayFor(model => item.ProductId)</td>
        <td height="100">@Html.DisplayFor(model => item.Quantity)</td>
        <td height="100">@Html.DisplayFor(model => item.tblProductList.uni)</td>
        <td height="100">@Html.DisplayFor(model => item.Discount)</td>
        <td height="100">@Html.DisplayFor(model => item.Total)</td>
    </tr>
}

我已将此添加到视图中:

@model IEnumerable<GridViewFuction.Models.ViewModel.OrderCustomerProductViewModel>

错误:ICollection 不包含 ProductId 和 UnitPrice 的定义

如果你能帮助我,那将是一个很大的帮助。

问题出在您访问数据的方式上。下面一行

 ProductId = v.tblProducts.ProductId

你的一侧有一个 int,另一侧有一个 List<int>,所以它在 UnitPrice

我不知道你想如何显示模型,但我会给出一些选项

和你的模型一样使用

ProductId = v.tblProducts.First().ProductId

正在将您的模型更改为此

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

然后使用

ProductId = v.tblProducts.Select(m => m.ProductId).ToList()

所以你的结果现在看起来像:

var results = (tblOrders.Where(l => l.OrderNo == id)
            .Select(v => new OrderCustomerProductViewModel
            {
                OrderNo = v.OrderNo,
                CustomerName = v.tblCustomer.CustomerName,
                ProductId = v.tblProducts.Select(m => m.ProductId).ToList(),
                Quantity = v.Quantity,
                UnitPrice = v.tblProducts.Select(m => m.UnitPrice).ToList(),
                Discount = v.Discount,
                Total = v.Total,
            })).ToList();

我上面显示的是单个客户和客户的产品,但是如果你想显示单个客户的单个产品和价格,你必须

  1. 假设每个Order有一个UnitPrice和一个Product,首先获取产品的计数并在创建时循环遍历它你的模型。

编辑:

//first create a list of your view model
var resultList = new List<OrderCustomerProductViewModel>();
var results = tblOrders.Where(l => l.OrderNo == id).ToList();
//the above code can be var results = tblOrder.toList(); if you need all orders
//now loop through the results above
foreach (var order in results)
{
    var products = order.tblProducts;
    foreach (var product in products)
    {
        resultList.Add(new OrderCustomerProductViewModel
        {
            OrderNo = order.OrderNo,
            CustomerName = order.tblCustomer.CustomerName,
            ProductId = product.ProductId,
            Quantity = order.Quantity,
            UnitPrice = product.UnitPrice,
            Discount = order.Discount,
            Total = order.Total,
        });
    }    
}
//then use the resultList
//NOTE that I went with the above assumption
  1. 有些Orders没有Product和/或UnitPrice,取[=21中计数最高的那个=] 和 UnitPrice 并使用循环计数。

我不能在这里写完整的代码。