【问题标题】:Compare two lists and pass it to View比较两个列表并将其传递给 View
【发布时间】:2020-11-25 00:35:30
【问题描述】:

基于 ASP.NET Core 3.0 的电子商务网站

有两个模型类:

  1. 产品
  2. 图片

单个产品的多个图像存储在图像表中。我正在尝试创建一个所有产品页面,但我正在努力将图像与产品代码匹配并将其传递给 View 以显示商店中的所有产品以及来自 Images.cs 和产品标题的缩略图及其价格Product.cs 表。我将如何匹配来自两个模型类的数据,并确保所有匹配的图像和产品都得到相关显示。

图像.cs

public class Image
    {

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ImageID { get; set; }
        public string ProductCode { get; set; }
        public virtual Product Product { get; set; }
        public byte[] Pic { get; set; }
        public string FileName { get; set; }
        public string ContentType { get; set; }

    }

产品.cs

public class Product
    {
        public Product()
        {
            ICollection<Category> Categories = new HashSet<Category>();
            ICollection<Image> Images = new HashSet<Image>();
        }
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ProductID{ get; set; }
        [Required, StringLength(150)]
        public string ProductCode{ get; set; }
        [Required, StringLength(150)]
        public string Title { get; set; }

        [StringLength(500)]
        public string Description { get; set; }
        
        public int Price { get; set; }

        [StringLength(150)]
        public string Type { get; set; }

        [ForeignKey("Category")]
        [Required]
        public int CategoryId { get; set; }
        public virtual Category Category { get; set; }

        public virtual ICollection<Category> Categories { get; set; }
        public virtual ICollection<Image> Images { get; set; }

    }

想要创建这样的东西:

【问题讨论】:

  • 每个产品一张,还是多张?
  • 对于“类别”(sp?)您需要确定这些关系是一对一、一对多还是多对多关系。现在,您正在使用外键作为类别,这似乎是一对一的,但是您也有一个类别列表。 docs.microsoft.com/en-us/ef/core/modeling/…

标签: asp.net-core entity-framework-core


【解决方案1】:

这样的?

如果您在数据库中的产品和图像之间存在关系,

var list = _context.Product.Include(x => x.Image).ToList();

如果你没有关系,

var imageslist = _context.Image;
var list = _context.Product.Select(x => new Product()
          {
              ProductId = x.ProductId,
              Title = x.Title,
              Price = x.Price,
              Description = x.Description,
              Images = imageslist.Where(y => y.ProductCode == x.ProductCode).ToList()
          }).ToList();

然后将 list 传递给 view 并在你的 view 中使用 @model List 来显示所有产品。

编辑 2

您的查看页面

@model IEnumerable<Product>

@{
    foreach (var item in Model)
    {
        var base64image = Convert.ToBase64String(item.Images.FirstOrDefault().Pic);
        <div class="col-xs-6 col-sm-4 col-md-3 col-lg-3">
            <table>
                <tr>
                    <td>
                        <img src="data:image/png;base64,@base64image" height="100">
                    </td>
                </tr>
                <tr>
                    <td>@item.Title</td>
                </tr>
                <tr>
                    <td>@item.Price</td>
                </tr>
            </table>
        </div>
    }
}

【讨论】:

  • 我将图像保存到 Byte 的数据库中。所以它需要根据产品代码将每个图像转换回字符串格式。
  • 我将图像保存到 Byte 的数据库中。所以它需要根据产品代码将每个图像转换回字符串格式。他们是一种关系。在视图中,我应该写什么来通过这个列表。 @model IEnumerable&lt;Product&gt; ??或@modelIEnumerable&lt;Image&gt;,你怎么写?表示我应该如何将此列表传递给视图??
  • 我更新了我的帖子。检查“编辑2”。类似的东西。
  • 更改了我的帖子以适合您的屏幕截图。 4 列。
  • 我已经标记了它,但由于声誉较低,它没有被考虑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-06
  • 1970-01-01
相关资源
最近更新 更多