我正在开发一个包含多选下拉列表的 MVC 应用程序。我想获取下拉列表中多个选定项目的 ID。
我有模型中的代码
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; } }
}
}
控制器代码是
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);
}
}
}
我没有在 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>
我想CustomerName list
在 View 中显示,所以我可以选择多个客户名称并将这些选定的客户 ID 传递回控制器。怎么做?