私のモデルがドロップダウンリストに正しくバインドされていない理由はわかりませんが、私はこれをいくつかの他のスポットで成功させましたが、ここでは役に立たない。これはショッピングカートのためのもので、カートを更新する数量ドロップダウンリストが必要で、データベースから現在の数量を表示します。MVC5モデルドロップダウンリストへのバインド
モデル:量のため
public class CartViewModel
{
public List<CategoryModel> Categories { get; set; }
public CartModel Cart { get; set; }
public IEnumerable<SelectListItem> Quantities { get; set; }
}
public class CartModel
{
public List<CartItem> Items { get; set; }
}
public class CartItem
{
public int ProductId { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public float Price { get; set; }
public float TotalPrice { get; set; }
public string ImageName { get; set; }
}
クラス、0-10
public class QuantityList
{
public static IEnumerable<SelectListItem> QtyList = new List<SelectListItem>()
{
new SelectListItem() {Text="0", Value="0" },
new SelectListItem() {Text="1", Value="1" },
new SelectListItem() {Text="2", Value="2" },
new SelectListItem() {Text="3", Value="3" },
new SelectListItem() {Text="4", Value="4" },
new SelectListItem() {Text="5", Value="5" },
new SelectListItem() {Text="6", Value="6" },
new SelectListItem() {Text="7", Value="7" },
new SelectListItem() {Text="8", Value="8" },
new SelectListItem() {Text="9", Value="9" },
new SelectListItem() {Text="10", Value="10" }
};
}
ビュー
@using (Html.BeginForm("UpdateCart", "Cart", FormMethod.Post))
{
for (int i = 0; i < Model.Cart.Items.Count; i++)
{
<div class="CartItem">
<span class="ItemName">@Model.Cart.Items[i].Name</span>
**<span class="ItemQty">@Html.DropDownListFor(m => Model.Cart.Items[i].Quantity, Model.Quantities)</span>**
<span class="RemoveItem">@Html.ActionLink("Remove", "RemoveItem", new { ProductId = Model.Cart.Items[i].ProductId })</span>
</div>
}
}
すべての負荷およびドロップダウンリストを除き、正しくレンダリングし、私もしましたTextBoxForを使用して、適切な値で正しく描画しますが、事前に設定されたListにバインドしようとしているときは描画されませんselectlistitems
ことができます正しく動作していなかったため、以下のスティーブンの答えは意図したとおりに働いていました。ご協力いただきありがとうございます –