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

时间:2019-06-30 01:33:00

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

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

有3个表(tblProduct, tblCustomer, tblOrder)。 tblOrdertblProduct是多对多的,而tblCustomertblOrder是一对多的。

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

我先使用Entity Framework数据库(所以我没有用于多对多关系的中间表)。

请帮助我。我是MVC的新手。预先感谢。

这些是我的桌子:

Product表:

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

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

Customer表:

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

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

Order表:

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的定义

如果您能帮助我,将会有很大帮助。

1 个答案:

答案 0 :(得分:1)

您访问数据的方式就是问题所在。下面的行

 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没有ProductUnitPrice ,得到Product和{{ 1}},然后将计数用于循环。

我不能在这里写下整个代码。