簡體   English   中英

MVC5模型綁定到DropdownList

[英]MVC5 Model Binding to DropdownList

我不知道為什么我的模型沒有正確綁定到下拉列表,我已經在其他幾個地方成功完成了此操作,但這里無濟於事。 這是用於購物車的,只需要一個數量下拉列表來更新購物車,並顯示數據庫中的當前數量。

模型:

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

}

除dropdownlist之外,所有內容均正確加載並呈現,我什至使用了TextBoxFor並以正確的值正確呈現,但在嘗試綁定到預填充的selectlistitems列表時卻無法正確呈現

這是在for循環內使用DropDownListFor()的不幸限制DropDownListFor()在CodePlex上已多次報道)。 您需要將EditorTemplate用作typeof CartItem並使用AdditionalViewDataSelectList傳遞給它,或者需要在每次迭代中生成一個新的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)
    ....
}

請注意,您可以將代碼簡化為

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 model.Quantities = Enumerable.Range(0, 10); 並認為

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

除了將列表綁定到視圖模型外,還可以直接轉到靜態列表:

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

因為您可以綁定定義了列表的任何內容。 至於默認設置為何無效的原因,我看不到將QtyList靜態變量傳遞到視圖模型的Quantities屬性的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM