2016-05-09 9 views
0

私のモデルがドロップダウンリストに正しくバインドされていない理由はわかりませんが、私はこれをいくつかの他のスポットで成功させましたが、ここでは役に立たない。これはショッピングカートのためのもので、カートを更新する数量ドロップダウンリストが必要で、データベースから現在の数量を表示します。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

答えて

1

これはforループ内にDropDownListFor()を使用するという残念な制限です(CodePlexで数回報告されています)。種類がCartItemの場合はEditorTemplateを使用し、AdditionalViewDataを使用してSelectListを渡すか、各繰り返しで新しいSelectListを生成してSelectedプロパティを設定する必要があります。

for (int i = 0; i < Model.Cart.Items.Count; i++) 
{ 
    .... 
    @Html.DropDownListFor(m => m.Cart.Items[i].Quantity, new SelectList(Model.Quantities, "Value", "Text", Model.Cart.Items[i].Quantity) 
    .... 
} 

あなたは私がViewModelにに取り付ける前にこれを試してもいた

public class CartViewModel 
{ 
    public List<CategoryModel> Categories { get; set; } 
    public List<CartItem> Items { get; set; } 
    public int[] Quantities { get; set; } 
} 

にあなたのコードを簡素化し、model.Quantities = Enumerable.Range(0, 10);を使用してQuantitiesを移入し、ビュー内の

@Html.DropDownListFor(m => m.Items[i].Quantity, new SelectList(Model.Quantities, Model.Items[i].Quantity) 
0

代わりのviewmodelにリストを結合させる、あなたの静的リストに直接行くことができる:

@Html.DropDownListFor(m => Model.Cart.Items[i].Quantity, QuantityList.QtyList) 

あなたが定義されたリストを持っている何かをバインドすることができたよう。なぜデフォルトが機能しないかについては、QtyList静的変数がビューモデルのQuantitiesプロパティに渡されるコードは表示されません。

+0

ことができます正しく動作していなかったため、以下のスティーブンの答えは意図したとおりに働いていました。ご協力いただきありがとうございます –

関連する問題