0

我有这 3 个课程:

 public class Product 
     {
            public int ProductId { get; set; }
            public int ClassId { get; set; }
            public string Name { get; set; }
            public virtual Class Class { get; set; }
        }



public class Partner
    {
        public int PartnerId { get; set; }
        public int ClassId { get; set; }     
        public string Name { get; set; }
        public virtual Class Class { get; set; }
    }



public class Price
    {
        public int PriceId { get; set; }
        public int ClassId { get; set; }
        public int ProductId { get; set; }
        public int PartnerId { get; set; }
        public float Cost { get; set; }
    }

我有 3 个数据列表:

List<Product> products; List<Partner> partners; List<Price> prices;

如何在 MVC 视图的表格单元格中显示顶行中的合作伙伴、左列中的产品和价格(如果每个合作伙伴和产品都可用)?

4

2 回答 2

4

与往常一样,我会首先设计一个反映视图要求的视图模型:

public class MyViewModel
{
    public IEnumerable<Product> Products { get; set; }
    public IEnumerable<PartnerPricesViewModel> PartnerProductPrices { get; set; }
}

public class PartnerPricesViewModel
{
    public string PartnerName { get; set; }
    public IEnumerable<Price> Prices { get; set; }
}

然后我将在我们的域模型的控制器操作中填充此视图模型并将其传递给视图:

public ActionResult Index()
{
    List<Product> products = ...
    List<Partner> partners = ...
    List<Price> prices = ...

    var model = new MyViewModel
    {
        Products = products,
        PartnerProductPrices =
            from partner in partners
            select new PartnerPricesViewModel
            {
                PartnerName = partner.Name,
                Prices = 
                    from product in products
                    select prices.FirstOrDefault(price => 
                        price.PartnerId == partner.PartnerId && 
                        price.ProductId == product.ProductId
                    )
            }
    };

    return View(model);
}

最后我将有一个对应于视图模型的强类型视图:

@model MyViewModel

<table>
    <thead>
        <tr>
            <th></th>
            @foreach (var product in Model.Products)
            {
                <th>@product.Name</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (var partner in Model.PartnerProductPrices)
        {
            <tr>
                <td>@partner.PartnerName</td>
                @foreach (var price in partner.Prices)
                {
                    <td>
                        @if (price != null)
                        {
                            @price.Cost.ToString("c")
                        }
                        else
                        {
                            // There was no price found matching this 
                            // product and partner => we display an empty cell
                            @:&nbsp;    
                        }
                    </td>
                }
            </tr>
        }
    </tbody>
</table>
于 2013-01-08T06:59:34.973 回答
0

您的prices列表对 SQL 非常友好,但并不真正适合实际问题。我认为您的价格应该存储在 aDictionary<Tuple<Product, Partner>, Price>而不是 a 中List<Price>

Since this is MVC, I would use that dictionary as part of the view-model (along with the products and partners). Then displaying it as a table is going to be very straightforward.

于 2013-01-08T07:06:57.660 回答