使用样本而不是下拉菜单时,来自 viewbag 的数据无法正确显示

Data from viewbag is not displaying properly when using a swatch instead a drop down menu

为此使用 C# ASP.NET MVC。

我正在处理产品详细信息页面。如果一个产品有多个变体,则会出现一个下拉列表,以便用户可以导航到其他产品。

到目前为止,这可以使用下拉列表,所有记录都在填充它。但现在我希望它与 SWATCH 一起工作。

我遇到的问题是,当我尝试使用填充下拉菜单的相同逻辑时,但对于 SWATCHES,页面不会 return 每种颜色。它只是 return您碰巧使用的产品的颜色。

我尝试过的解释

这是下拉菜单的代码。这正在工作并且正在填充页面。

<div class="form-group">
     <div class="col-md-6">
          @Html.DropDownListFor(model => model.id, new SelectList(ViewBag.myColorValues, "id", 
           "color"), null, new { @class = "form-control"})
      </div>
</div>

现在,当我尝试实现相同的逻辑以使用样本图标显示数据时,并非所有记录都像在下拉列表中那样显示。

<div class="product-size-variations">
   <p class="variation-label">Size:</p>
      <div class="product-size-variation variation-wrapper">
         <div class="swatch-wrapper">
            <a href="~/myproduct/@Model.id"
              class="product-size-swatch-btn selected bg-color" data-bg- 
                   color="@Model.ColorValue"
                   data-bs-placement="top" title="@Model.color">
                 <span class="product-size-variation-label"></span>
            </a>
          </div>
      </div>
    </div>

下拉逻辑的代码片段

控制器:

    public ActionResult Details(int? id)
        {                      
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product Product = db.Products.Find(id);
            if (Product == null)
            {
                return HttpNotFound();
            }

           
            ViewBag.myColorValues = db.Products.Where
                (x => x.isActive == true &&
                x.VariantGroupID == Product.VariantGroupID &&
x.color == Product.color && x.ColorValue == ColorValue).AsNoTracking().ToList();

            return PartialView("_nazhapdp", Product);
        }


    
     public PartialViewResult GetProductVariants(int? id, string VariantGroupID, string color, string ColorValue)
    {
        
        //This is for the drop down list
        ViewBag.LoadVariantColors = db.Products.Where(x => x.isActive == true &&
        x.VariantGroupID == VariantGroupID &&
        x.color == color && x.ColorValue == ColorValue).AsNoTracking().ToList();
       
        //Returning the data for when using the drop down or SWATCH
        return PartialView("_partialviewVariantsPDP", ViewBag.LoadVariantColors);      
    }

视图上的 Swatch 图标逻辑:

if (Model.isVariant == true && Model.VariantGroupID == VariantGroupID)
      {        
            @foreach (var record in Model.id.ToString())
            {
     <div class="product-size-variations">
       <p class="variation-label">Size:</p>
          <div class="product-size-variation variation-wrapper">
             <div class="swatch-wrapper">
                <a href="~/myproduct/@Model.id"
                  class="product-size-swatch-btn selected bg-color" data-bg- 
                       color="@Model.ColorValue"
                       data-bs-placement="top" title="@Model.color">
                     <span class="product-size-variation-label"></span>
                </a>
              </div>
          </div>
        </div>
            }            
      }

我错过了什么吗? return 将数据添加到下拉列表与 return 在页面上以另一种形式添加数据时,它的工作方式是否不同?对于下拉菜单,我使用 select 列表,对于色板,我只是将代码添加到 for 循环中,并且足够了解以确保这也应该有效。我在想这可能与循环有关,但无法弄清楚。

问题是您在控制器中对颜色进行过滤吗?它没有解释为什么您会得到四条记录,但问题似乎出在此处。

单步执行和调试应该可以确认问题。

 public PartialViewResult GetProductVariants(int? id, string VariantGroupID, string color, string ColorValueSelectionHashForPDP)
{
    
    //This is for the drop down list
    ViewBag.DynamicLoadVariantColors = db.Products.Where(x => x.isActive == true &&
    x.VariantGroupID == VariantGroupID &&
    // Are you filtering on only one colour here? Try removing this.
    x.color == color && 
    
    x.ColorValueSelectionHashForPDP == ColorValueSelectionHashForPDP).AsNoTracking().ToList();
   
    //Returning the data for when using the drop down or SWATCH
    return PartialView("_partialviewVariantsPDP", ViewBag.DynamicLoadVariantColors);      
}