是否可以在具有foreach条件的单元格中显示多个项目?

时间:2012-09-05 13:43:34

标签: database asp.net-mvc-3 database-design

What I have & what I would like

我认为使用图片可视化比我解释我想要的更容易。 有没有办法让一张桌子看起来像是图片上的第二张?现在我已经是第一张了,但我遇到的问题是我的表格动态填充如下:

@model IEnumerable<MyApp.Models.OrderDetail>
@foreach (var item in Model)
{
    <tr>

        <td align="center">
            @Html.DisplayFor(modelItem => item.OrderId)
        </td>

        <td>
         @Html.DisplayFor(modelItem => item.Order.Username)          
        </td>
        <td>
         @Html.DisplayFor(modelItem => item.Order.ClientID)          
        </td>

        <td>
         @Html.DisplayFor(modelItem => item.Order.SiteNumber)          
        </td>   

        <td>
           @{
            if (item.Pack == null)
            {
                   @Html.DisplayFor(modelItem => item.Product.Name) 
            }
            else
            {
                 <text>Pack</text>
                 @Html.DisplayFor(modelItem => item.Pack.Name)
            }
            }    
        </td>

        <td>
            @if (item.Pack == null)
            {
                @Html.DisplayFor(modelItem => item.UnitPrice)
            }
            else
            {
                @Html.DisplayFor(modelItem => item.UnitPricePack)
            }
        </td>

        <td align="center">
            @Html.DisplayFor(modelItem => item.Quantity)
        </td>

        <td>
            @Html.DisplayFor(modelItem => item.Order.Total)
        </td>

        <td>
            @Html.DisplayFor(modelItem => item.Order.OrderDate)
        </td>
    </tr>  
    .....

我在互联网上没有发现任何关于除了接口之外的内容,但在我的级别上看起来有点复杂,如果我使用它,我必须更改所有代码。一个主意 ?我接受了所有建议,谢谢你的帮助

以下是我的模特:

订单明细

 public class OrderDetail
    {
        public int OrderDetailId { get; set; }
        public int OrderId { get; set; }
        public int ProductId { get; set; }
        public int PackId { get; set; }
        public int Quantity { get; set; }

        [Range(0.00, 100000.00)]
        public decimal UnitPrice { get; set; }

        [Range(0.00, 100000.00)]
        public decimal UnitPricePack { get; set; }


        public virtual Product Product { get; set; }
        public virtual Pack Pack { get; set; }
        public virtual Order Order { get; set; }

        public virtual List<Product> Products { get; set; }
        public virtual List<Pack> Packs { get; set; }
    }

产品

 public class Product
    {
        [ScaffoldColumn(false)]
        public int      ProductId   { get; set; }

        [DisplayName("Pack")]
        public int      PackId      { get; set; }

        [Required(ErrorMessage="blabla")]
        [StringLength(160)]
        [DisplayName("Nom")]
        public string   Name        { get; set; }

        [DisplayName("Description")]
        [StringLength(200)]
        public string   Description { get; set; }

        [Required(ErrorMessage="blabla")]
        [Range(0.00, 10000.00, ErrorMessage="blabla")]
        [DisplayName("Prix")]
        public decimal   Price       { get; set; }

        public virtual Pack     Pack  { get; set; }

        public virtual List<OrderDetail> OrderDetails { get; set; }
    }

和我的包模型

 public class Pack
    {
        [ScaffoldColumn(false)]
        public int      PackId      { get; set; }

        [DisplayName("blabla")]
        public string   Name        { get; set; }

        [DisplayName("blabla")]
        public string   Description { get; set; }


        [Required(ErrorMessage="blabla")]
        [Range(0.00, 10000.00, ErrorMessage="blabla")]
        [DisplayName("Prix")]
        public decimal  Price { get; set; }

        public List<Product> Products { get; set; }
        public virtual List<OrderDetail> OrderDetails { get; set; }
    }

1 个答案:

答案 0 :(得分:3)

假设您有像这样的ViewModel

public class OrderViewModel
{
  public int OrderID { set;get;}
  public string UserName { set;get;}
  public int UserID { set;get;}
  public List<OrderItem> Products { set;get;}
  public decimal OrderTotal { set;get;}
}

public class OrderItem
{
  public string ProductID { set;get;}
  public string ProductName { set;get;}
  public decimal UnitPrice { set;get;}
  public int Quantity { set;get;}
}

现在在你看来

@model List<OrderViewModel>
@foreach(var order in Model)
{
  <tr>
     <td>@order.OrderID</td>
     <td>@order.UserName</td>
     <td>
        <ul>
        @foreach(var item in order.Products)
        {
          <li>@item.ProductName</li> 
        }
        </ul>
     </td>
     <td>
        <ul>
        @foreach(var item in order.Products)
        {
          <li>@item.Quantity</li> 
        }
        </ul>
     </td>
     <td>
        <ul>
        @foreach(var item in order.Products)
        {
          <li>@item.UnitPrice</li> 
        }
        </ul>
     </td>
     <td> 
         @order.OrderTotal
     </td>
 </tr>    
}