5

我正在开发一个包含多选下拉列表的 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 传递回控制器。怎么做?

4

1 回答 1

15

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.

于 2012-09-06T09:08:45.860 回答