是否有可能在同一个foreach条件的单元格中显示多个项目?(Is it possible to d

2019-09-22 03:58发布

我觉得它更容易与图片形象化比我解释了我想要的东西。 有没有办法有一个表,看起来像图片上的第二个? 现在,我的第一次,但我有问题,我的表是动态填充是这样的:

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

Answer 1:

假设你有一个这样的视图模型

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>    
}


文章来源: Is it possible to display more than one item in a cell with a foreach condition?