(コードに関して) 解決方法がわからない問題があり、コミュニティから意見を求めたいと思います。
そのため、製品の詳細を表示するページがあり、これらの詳細は、DropDownList (DDL) で選択された値によって異なります。したがって、各製品について、次のように、UniqueId および属性 uniqueId の JSON 配列によって識別される、その製品のバリエーションのコレクションがあります。
[
{
"AttributeId": "fe153d69-8ac1-6e0c-8793-ff0000804eb3",
"AttributeValueId": "64163d69-8ac1-6e0c-8793-ff0000804eb3"
},
{
"AttributeId": "00163d69-8ac1-6e0c-8793-ff0000804eb3",
"AttributeValueId": "67163d69-8ac1-6e0c-8793-ff0000804eb3"
}
]
これは、この特定のバリエーションにはDays
、値が 5、Portions
属性値が 4 などの属性があることを示しています。
この特定の製品には、同じスキームの他の値/バリエーションもあります。可能な値は次のとおりです。
Days | Portions
3 2
3 4
3 6
5 4
現在、私のビューでは、これらの属性のそれぞれに 2 つの DDL をバインドしています (1 つは日用、もう 1 つはポーション用)。
これが私のコントローラーメソッドです:
public ActionResult Details(Guid productId)
{
CatalogManager catalogManager = CatalogManager.GetManager();
EcommerceManager ecommerceManager = EcommerceManager.GetManager();
ProductListWidgetModel model = new ProductListWidgetModel();
model.Product = catalogManager.GetProduct(productId);
//Read the variant properties
JavaScriptSerializer serializer = new JavaScriptSerializer();
model.ProductVariations = catalogManager.GetProductVariations(productId).ToList();
model.Portions = new List<SelectListItem>();
model.Meals = new List<SelectListItem>();
foreach (var item in model.ProductVariations)
{
List<AttributeValuePair> attributeValuePairs = serializer.Deserialize<List<AttributeValuePair>>(item.Variant);
foreach (var attribute in attributeValuePairs)
{
if (catalogManager.GetProductAttribute(attribute.AttributeId).Title == "Portions")
{
var attributes = catalogManager.GetProductAttributeValue(attribute.AttributeValueId);
if (!model.Portions.Exists(x => x.Value == attributes.Title))
{
model.Portions.Add(new SelectListItem { Value = attributes.Id.ToString(), Text = attributes.Title });
}
}
if (catalogManager.GetProductAttribute(attribute.AttributeId).Title == "Days")
{
var attributes = catalogManager.GetProductAttributeValue(attribute.AttributeValueId);
if (!model.Meals.Exists(x => x.Value == attributes.Title))
{
model.Meals.Add(new SelectListItem { Value = attributes.Id.ToString(), Text = attributes.Title });
}
}
}
}
return View("Detail", model);
}
そして私のモデル:
public class ProductListWidgetModel
{
/// <summary>
/// Gets or sets all the messages.
/// </summary>
public string Message { get; set; }
public string Title { get; set; }
public string SubTitle { get; set; }
public Product Product { get; set; }
public List<Product> ProductsList { get; set; }
public List<ProductVariation> ProductVariations { get; set; }
public List<SelectListItem> Portions { get; set; }
public List<SelectListItem> Meals { get; set; }
}
また、私の見解:
<table>
<tr>
<th id="row">@Html.DropDownList("MealsSelector", @Model.Meals } )</th>
<td id="row">Middager</td>
</tr>
<tr>
<th id="row">@Html.DropDownList("PortionsSelector", @Model.Portions)</th>
<td id="row">Porsjoner</td>
</tr>
</table>
しかし、私が実際に行う必要があるのは、ポーションにバインドされた DDL に、日 DDL に基づいて可能な値のみを表示させることです。したがって、5 日を選択した場合、ポーション DDL で 4 つのポーションを選択する選択肢しかないはずです。
どうすればこれを達成できますか?理想的には、これを行うには Ajax または単純な LINQ フィルターを使用することをお勧めします。
これらの DDL を相互にバインドする方法を教えてくれる人はいますか?