簡體   English   中英

如何在ASP.Net MVC中顯示多個DropDownLists並獲取值

[英]How to display multiple DropDownLists and get values in ASP.Net MVC

我正在尋找一種通過循環創建下拉列表,然后將值存儲在模型中的簡單方法。

查看模型:

public class PreferencesShoppingCartViewModel
            {
                public List<CartItemPreference> CartItemPreferences{get;set;}

                public class CartItemPreference{
                      public string Selected{get;set;}
                      public CartItem CartItem {get;set;}
                }
            }

@model MVC_COMP1562.ViewModels.PreferencesShoppingCartViewModel

視圖:

@model MVC_COMP1562.ViewModels.PreferencesShoppingCartViewModel

@foreach (var cartItem in Model.CartItemPreferences)
{
    @Html.DropDownListFor(m => cartItem.Selected, Model.PreferenceOptions)

}

每個下拉列表的ID均相同。

因此,我正在為cartItemPreferences中的每個項目創建div btn-group,但是現在如何在Model中將選擇的值分配給適當的CartItemPreference?

您需要使用for循環而不是foreach ,以便每個dropdownlist具有唯一的ID。

<form action="/" method="post">
   <select id="CartItemPreferences_0__Selected" name="CartItemPreferences[0].Selected">
      <option value="1">Option 1</option>
      <option value="2">Option 2</option>
      <option value="3">Option 3</option>
   </select>
   <select id="CartItemPreferences_1__Selected" name="CartItemPreferences[1].Selected">
      <option value="1">Option 1</option>
      <option value="2">Option 2</option>
      <option value="3">Option 3</option>
   </select>
   <input type="submit" value="Submit"/>
</form>

模型

public class PreferencesShoppingCartViewModel
{
    public List<CartItemPreference> CartItemPreferences { get; set; }

    public List<SelectListItem> PreferenceOptions { get; set; }

    public PreferencesShoppingCartViewModel()
    {   
        CartItemPreferences = new List<CartItemPreference>();
        PreferenceOptions = new List<SelectListItem>();
    }
}

public class CartItemPreference
{
    public string Selected { get; set; }
    public CartItem CartItem { get; set; }
}

public class CartItem
{
    public string Sample { get; set; }
}

視圖

@model DemoMvc5.Models.PreferencesShoppingCartViewModel
@{
    ViewBag.Title = "Home Page";
}

@using (Html.BeginForm("Index", "Home"))
{
    for (int i = 0, length = Model.CartItemPreferences.Count; i < length; i++)
    {
        @Html.DropDownListFor(m => Model.CartItemPreferences[i].Selected, Model.PreferenceOptions)
    }
    <input type="submit" value="Submit"/>
}

控制者

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new PreferencesShoppingCartViewModel
        {
            CartItemPreferences = new List<CartItemPreference>
            {
                new CartItemPreference { CartItem = new CartItem { Sample = "Sample 1"}},
                new CartItemPreference { CartItem = new CartItem { Sample = "Sample 2"}}
            },
            PreferenceOptions = new List<SelectListItem>
            {
                new SelectListItem {Text = "Option 1", Value = "1"},
                new SelectListItem {Text = "Option 2", Value = "2"},
                new SelectListItem {Text = "Option 3", Value = "3"}
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(PreferencesShoppingCartViewModel model)
    {
        // Do something

        return View(model);
    }
}

屏幕截圖

在此處輸入圖片說明

在此處輸入圖片說明

暫無
暫無

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

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