I'm working on add to Cart in Shoe Shopping. Here is my problem. I design one product have many size and many color in database (I'm not sure it correct). When I choose the shoe for add to cart. I want the user to choose a color then the size of that shoe will appear, here the view
Because the product has different Id but same name with different color and size so I use linq to get all the color of that product and use viewbang to send to the view
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;
Here the Razor view
@{
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>
Is there any way to choose the color and then shoes size? Because my code when I chose the color and then select the size its change to different products. Does my database design is wrong?. I think my problem is the code logic when I group by the name of product. Is there any solutions to add product with color and size correspond. Here the product table and relationship with size and color