2012-09-06 76 views
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); 
      } 
    } 
} 

我沒有得到什麼,在查看寫,我已經嘗試了不同的代碼,但我越來越混亂.. 。

代碼在瀏覽:

@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,因此我可以選擇多個客戶名稱並將選定的客戶ID傳回給控制器。 如何做到這一點?

+0

你可以發佈你迄今在視圖中試過的東西嗎?您的視圖是否將「CustomerList」作爲預期格式的模型引用?例如'@model IList '。您還應該在代碼中聲明該列表爲「IList 」,而不是「列表」。 – Nope

+0

我已經使用下面的代碼。 '@model CustomerDEMOForMultiselect.Models.Customer @ { Layout = null; } <!DOCTYPE HTML> DisplayCustomer

@using (Html.BeginForm()) { @Html.DropDownListFor(v => v.ID,new MultiSelectList(ViewBag.CustList,"ID","Name",ViewBag.ID))
}
' – bnil

+0

我添加的代碼到你的問題,更容易閱讀:) – Nope

回答

15

使用包裝模型的屬性來選擇客戶綁定工作(我試了一下):

包裝型號:

public class CustomerList 
{ 
    public List<Customer> Customers { get; set; } 
    public List<int> SelectedIDs { get; set; } 
} 

控制器:

 [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 
     } 

查看:

@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" /> 
} 

您需要將您的選擇綁定到控制器並將其發送回控制器。

+0

謝謝娜塔莉•克倫貝格爾在哪裏添加這個包裝類?在客戶模型本身? – bnil

+0

是否必須添加強類型視圖?我創建了空視圖。我在'@ Model.Customer'上出現錯誤,錯誤是'System.Web.Mvc.MultiSelectList.MultiSelectList(System.Collections.IEnumerable,string,string,System.Collections.IEnumerable)'has一些無效的參數 – bnil

+0

謝謝...它的工作原理.. – bnil

相關問題