[英]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 数量分类,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 除dropdownlist之外,所有内容均正确加载并呈现,我什至使用了TextBoxFor并以正确的值正确呈现,但在尝试绑定到预填充的selectlistitems列表时却无法正确呈现
This is an unfortunate limitation of using DropDownListFor()
inside a for
loop (its been reported a few times on CodePlex). 这是在
for
循环内使用DropDownListFor()
的不幸限制DropDownListFor()
在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. 您需要将
EditorTemplate
用作typeof CartItem
并使用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)
....
}
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);
并使用
model.Quantities = Enumerable.Range(0, 10);
填充Quantities
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.
至于默认设置为何无效的原因,我看不到将QtyList静态变量传递到视图模型的Quantities属性的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.