代码之家  ›  专栏  ›  技术社区  ›  NghÄ©a NghÄ©a

如何按颜色选择鞋码ASP。NET核心

  •  0
  • NghÄ©a NghÄ©a  · 技术社区  · 2 年前

    我正在努力添加到购物车。这是我的问题。我在数据库中设计了一个有很多尺寸和颜色的产品(我不确定它是否正确)。当我选择要添加到购物车的鞋子时。我想让用户选择一种颜色,然后显示鞋子的尺寸,这是视图

    enter image description here

    因为产品有不同的Id,但名称相同,颜色和大小不同,所以我使用linq获取该产品的所有颜色,并使用viewbang发送到视图

    var sizes = _context.Products.Where(n => n.name== sp.name)
        .Include(s => s.ColorNavigation)
        .Include(s => s.SizeNavigation)
        .GroupBy(n=>n.SizeNavigation.Size1)
        .Select(n => n.FirstOrDefault())
        .ToList();
    ViewBag.ListSizes = sizes;
    

    这是Razor视图

    @{
        IEnumerable<SanPham> ListSize = ViewBag.ListSizes;
    }
    
    <div class="product-options">
                    <label>
                        Color
                        <select class="input-select" onchange="redirectToSelectedOption(this)">
                            <option value="0">@Model.ColorNavigation.Color1</option>
                            @foreach (var item in ViewBag.ListSizes)
                            {
                                if (item.ColorNavigation.Color1 != Model.ColorNavigation.Color1)
                                {
                                    <option data-url="/post/@(SlugGenerator.SlugGenerator.GenerateSlug(item.Hinhanh1)) - @(item.Idsp)">@item.ColorNavigation.Color1</option>
                                }
    
                            }
                        </select>
                    </label>
    
                    <label>
                        Size
                        <select class="input-select" onchange="redirectToSelectedOption(this)">
                            <option >@Model.SizeNavigation.Size1</option>
                            @foreach (var item in ViewBag.ListSizes)
                            {
                                if (item.SizeNavigation.Size1 != Model.SizeNavigation.Size1)
                                {
                                    <option  data-url="/post/@(SlugGenerator.SlugGenerator.GenerateSlug(item.Hinhanh1)) - @(item.Idsp)">@item.SizeNavigation.Size1</option>
                                }
                                
                            }
                        </select>
    
                    </label>
    
    </div>
    

    有没有办法选择颜色,然后选择鞋子的尺码?因为我的代码当我选择颜色,然后选择尺寸时,它会改变为不同的产品。我的数据库设计错了吗?。我认为我的问题是当我按产品名称分组时的代码逻辑。是否有任何解决方案可以添加颜色和尺寸对应的产品。 这里的产品表以及与尺寸和颜色的关系 enter image description here

    0 回复  |  直到 2 年前
        1
  •  1
  •   Md Farid Uddin Kiron    2 年前

    有没有办法选择颜色,然后选择鞋子的尺码?因为我的 代码时,我选择了颜色,然后选择了其更改为的大小 不同的产品。我的数据库设计错了吗。

    是的。关于你的数据库设计,很难评论它是正确的还是错误的,因为我们不知道要求是什么。然而,对于这种场景,或者我们可以说对于电子商务购物车的设计,我们通常按产品表、产品变体表进行设计。在产品变体中,我们包括颜色、尺寸、SKU、价格和数量。

    因此,我们不能说你的设计是错误的,可能是你按照要求做的。但如果有一个变体表,或者产品的颜色和尺寸列表,我们仍然可以根据产品的尺寸过滤产品。

    为了利用您的产品ERD,我将您的产品表写如下:

    public class Product
    {
        public int ProductID { get; set; }
        public string Name { get; set; }
        public List<string> Colors { get; set; }
        public List<string> Sizes { get; set; }
    }
    

    笔记 为了执行我这样做的测试,你可以根据你的要求进行修改。

    现在,让我们假设您在数据库中有以下产品列表:

    List<Product> products = new List<Product>
         {
             new Product { ProductID = 1, Name = "Nike Shoe", Colors = new List<string>{"Black", "White"}, Sizes = new List<string>{"8", "9", "10"}},
             new Product { ProductID = 2, Name = "Adidas T-Shirt", Colors = new List<string>{"Red", "Blue", "Green"}, Sizes = new List<string>{"S", "M", "L"}},
             new Product { ProductID = 3, Name = "Puma Jacket", Colors = new List<string>{"Black", "Grey"}, Sizes = new List<string>{"M", "L"}},
             new Product { ProductID = 4, Name = "Reebok Shorts", Colors = new List<string>{"Blue", "Grey"}, Sizes = new List<string>{"S", "M", "L"}},
             new Product { ProductID = 5, Name = "New Balance Sneakers", Colors = new List<string>{"Red", "Blue"}, Sizes = new List<string>{"9", "10", "11"}},
             new Product { ProductID = 6, Name = "Under Armour Hoodie", Colors = new List<string>{"Grey", "Black"}, Sizes = new List<string>{"S", "M", "L"}},
             new Product { ProductID = 7, Name = "Fila Pants", Colors = new List<string>{"Black", "Navy"}, Sizes = new List<string>{"M", "L", "XL"}},
             new Product { ProductID = 8, Name = "Converse Shoes", Colors = new List<string>{"White", "Black"}, Sizes = new List<string>{"7", "8", "9"}},
             new Product { ProductID = 9, Name = "Vans Sneakers", Colors = new List<string>{"Black", "Checkerboard"}, Sizes = new List<string>{"8", "9", "10"}},
             new Product { ProductID = 10, Name = "Asics Running Shoes", Colors = new List<string>{"Blue", "Yellow"}, Sizes = new List<string>{"8", "9", "10"}},
         };
    

    现在,我想获得每种特定产品的所有尺寸。这样我就可以像下面这样做:

    string searchName = "Nike Shoe";
    string searchColor = "Black";
    
              
    var sizes = products
        .Where(p => p.Name == searchName && p.Colors.Contains(searchColor))
        .SelectMany(p => p.Sizes)
        .Distinct()
        .ToList();
    
    
    Console.WriteLine($"Sizes for {searchName} in {searchColor}:");
    foreach (var size in sizes)
    {
        Console.WriteLine(size);
    }
    

    备选方式:

    您也可以尝试此查询:

     var sizes = products
          .Where(p => p.ProductName == searchName && p.Color.ColorName == searchColor)
          .Select(p => p.Size.SizeName)
          .Distinct()
          .ToList();
    

    enter image description here

    输出

    enter image description here

    enter image description here

    笔记 这是其中一种方式,也可能有其他方式,这取决于表的设计和偏好。