2

我有什么和我想要什么

我认为用图片来形象化比解释我想要的更容易。有什么办法可以让一张桌子看起来像图片上的第二张?现在我是第一个,但我遇到的问题是我的表是这样动态填充的:

@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; }
    }
4

1 回答 1

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>    
}
于 2012-09-05T14:01:09.000 回答