5

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.

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...

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 list in View , so I can select multiple customer name and pass those selected customer ID's back to controller. How to do that?

tereško
  • 58,060
  • 25
  • 98
  • 150
bnil
  • 1,531
  • 6
  • 35
  • 68
  • Can you post what you have tried so far in the view? Does your view reference the `CustomerList` as a model in the expected format? For example `@model IList`. You should also declare the list as an `IList` instead of `List` in your code. – Nope Sep 06 '12 at 08:46
  • I have used following Code. `@model CustomerDEMOForMultiselect.Models.Customer @{ Layout = null; } DisplayCustomer
    @using (Html.BeginForm()) { @Html.DropDownListFor(v => v.ID,new MultiSelectList(ViewBag.CustList,"ID","Name",ViewBag.ID))
    }
    `
    – bnil Sep 06 '12 at 09:13
  • I added that code to your question, easier to read :) – Nope Sep 06 '12 at 09:22

1 Answers1

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.

  • Thanks Nathalie Kellenberger, Where to add this wrapper class ? In customer model itself ? – bnil Sep 06 '12 at 09:17
  • Whether I have to add view strongly typed ? I have created Empty view.I am geeting an error on `@Model.Customer` And error is 'System.Web.Mvc.MultiSelectList.MultiSelectList(System.Collections.IEnumerable, string, string, System.Collections.IEnumerable)' has some invalid arguments – bnil Sep 06 '12 at 09:22
  • 2
    As a side-note, try to use the interfaces when declaring your properties instead of the conrecte implementations. i.e: Change `public List Customers { get; set; }` to `public IList Customers { get; set; }` and so on. – Nope Sep 06 '12 at 09:34
  • The wrapper class is a model itself therefore you add it to the model folder. and you need to have a strongly typed view. – Nathalie Kellenberger Sep 06 '12 at 10:07
  • With this solution, we get the selected items from MultiSelectList. How can I get all items in MultiSelectList? I am using MultiSelectList with a list dynamically changing. – Tunahan Nov 13 '18 at 08:15