简体   繁体   中英

MVC5 Model Binding to DropdownList

I don't know why my model isn't binding to a drop-down list correctly, i've done this in a few other spots successfully, but no avail here. This is for a shopping cart, just want a quantity dropdownlist that will update the cart, and display the current quantity from the database.

Model:

    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; }
}

Class for quantities, 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" }
    };
}

View

    @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>
    }

}

everything loads and renders correctly, except for the dropdownlist, i've even used a TextBoxFor and it renders correctly with the proper values, but not when trying to bind to the pre-populated List of selectlistitems

This is an unfortunate limitation of using DropDownListFor() inside a for loop (its been reported a few times on CodePlex). You need to either use an EditorTemplate for typeof CartItem and pass your SelectList to it using AdditionalViewData , or generate a new SelectList in each iteration and set the Selected property.

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)
    ....
}

Note that you can simplify your code to

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

and populate the Quantities using model.Quantities = Enumerable.Range(0, 10); and in the view

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

Instead of binding the list to the viewmodel, you could go directly to your static list:

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

As you can bind anything that has the list defined. As far as why the default doesn't work, I don't see the code where the QtyList static variable is passed to the view model's Quantities property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM