asp.net-mvc 如何将多选列表的选定项目传回控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12295199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass multiselect list's selected Items back to controller?
提问by bnil
I am developing a MVC application which contains the multiselect dropdown list. I want to get the ID's of multiple selected items of the drop down.
我正在开发一个包含多选下拉列表的 MVC 应用程序。我想获取下拉列表中多个选定项目的 ID。
I have the code in model
我有模型中的代码
namespace CustomerDEMOForMultiselect.Models
{
public class Customer
{
private int _ID;
private string _Name;
private double _Amt;
public int ID { get { return _ID; } set { _ID = value; } }
public string Name { get { return _Name; } set { _Name = value ; } }
public double Amt { get { return _Amt; } set { _Amt = value; } }
}
}
And Controller Code is
控制器代码是
namespace CustomerDEMOForMultiselect.Controllers
{
public class CustomerController : Controller
{
public ActionResult DisplayCustomer()
{
Customer oCustomer = new Customer();
List<Customer> CustomersList = new List<Customer>();
CustomersList.Add(new Customer() { ID = 1, Name = "TestCustomer1", Amt = 123 });
CustomersList.Add(new Customer() { ID = 2, Name = "TestCustomer2", Amt = 234 });
CustomersList.Add(new Customer() { ID = 3, Name = "TestCustomer3", Amt = 324 });
ViewBag.CustList = CustomersList;
return View(CustomersList);
}
}
}
I am not getting what to write in View, I have tried different code but I am getting confusing...
我没有在 View 中写什么,我尝试了不同的代码,但我越来越困惑......
Code in View:
视图中的代码:
@model CustomerDEMOForMultiselect.Models.Customer
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>DisplayCustomer</title>
</head>
<body>
<div>
@using (Html.BeginForm())
{
@Html.DropDownListFor(v => v.ID, new MultiSelectList(ViewBag.CustList,"ID","Name",ViewBag.ID))
<br />
<input type="submit" value="Submit" />
}
</div>
</body>
</html>
I want to show the CustomerName listin View , so I can select multiple customer name and pass those selected customer ID's back to controller.
How to do that?
我想CustomerName list在 View 中显示 ,因此我可以选择多个客户名称并将这些选定的客户 ID 传递回控制器。怎么做?
回答by Nathalie Kellenberger
Using a wrapper model with a property to bind the selected customers to works (I tried it):
使用带有属性的包装器模型将选定的客户绑定到作品(我尝试过):
Wrapper Model:
包装型号:
public class CustomerList
{
public List<Customer> Customers { get; set; }
public List<int> SelectedIDs { get; set; }
}
Controller:
控制器:
[HttpGet]
public ActionResult DisplayCustomer()
{
Customer oCustomer = new Customer();
List<Customer> CustomersList = new List<Customer>();
CustomersList.Add(new Customer() { ID = 1, Name = "TestCustomer1", Amt = 123 });
CustomersList.Add(new Customer() { ID = 2, Name = "TestCustomer2", Amt = 234 });
CustomersList.Add(new Customer() { ID = 3, Name = "TestCustomer3", Amt = 324 });
ViewBag.CustList = CustomersList;
return View(new CustomerList() { Customers = CustomersList });
}
[HttpPost]
public void DisplayCustomer(List<int> selectedIds)
{
// do something with the id list
}
View:
看法:
@model MvcApplication2.Models.CustomerList
@using (Html.BeginForm(@Model.SelectedIDs))
{
@Html.ListBoxFor(m => m.SelectedIDs, new MultiSelectList(@Model.Customers, "ID", "Name", @Model.SelectedIDs))
<input type="submit" value="save" />
}
You need something to bind your selection to and send it back to the controller.
您需要一些东西来绑定您的选择并将其发送回控制器。

