如何在 ASP.Net MVC 中显示多个 DropDownList 并获取值

海吉思

我正在寻找一种通过循环创建下拉列表的简单方法,然后将值存储在模型中。

查看型号:

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,但现在如何将选定的值分配给模型中正确的 CartItemPreference?

您需要使用for 循环而不是foreach,以便每个下拉列表都有唯一的 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);
    }
}

屏幕截图

在此处输入图片说明

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在ASP.NET MVC中获取当前用户

ASP.NET MVC中的DropdownList-未发布值

如何使用所需的验证创建ASP.Net MVC DropDownList

ASP.Net MVC5中DropDownlist的困难

使用CHOSEN在MVC ASP.Net中可搜索的DropDownList

如何在ASP.NET MVC中创建可搜索的DropDownList

如何在ASP.NET Core MVC App中获取DropDownList的选定值

如何从.net-MVC中的DropDownList获取文本?

ASP.NET MVC DropDownList替代

c#ASP.net MVC中的DropDownlist和列表问题

如何在jQuery方法中使用@razor呈现Dropdownlist-ASP.NET MVC

ASP.NET MVC的层叠DropDownList

如何在ASP.NET MVC中从View到JavaScript获取值

ASP.NET MVC DropDownList NullReferenceException

如何从ASP.NET MVC中的Html.ListBox获取值

如何在ASP.Net C#中的GridView的EditMode中显示DropdownList?

如何在asp.net MVC中获取登录的用户ID并在隐藏的文本框中显示它

在asp.net MVC中为dropdownlist创建列表

asp.net mvc不再从子类获取值

asp.net MVC如何在集合中显示值

如何在 .Net MVC Core 中将枚举显示为 DropDownList

从定义的 <td> (ASP.NET MVC) 中获取值

从 AJAX (ASP.NET MVC) 填充 DropdownList

如何使用 ASP.NET WebForms 中的 JavaScript 从 FormView 中的 DropDownList 获取文本到 TextBox 中?

在 ASP.NET MVC 中填充 DropDownList

无法在 ASP.NET MVC 的 DropDownList 中检索选定的值?

如何在 Asp.Net MVC 中显示上周的数据

如何使用 ASP.Net MVC 级联 DropDownList

从asp.net MVC控制器类中的SELECT标记中获取值的问题