I am developing an MVC application that contains a multi-select dropdown. I want to get the id of several selected dropdown list items.
I have code in the 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 the controller code
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 do not get what to write in View, I tried to use different code, but I get confused ...
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 CustomerName listin the view, so I can select multiple client names and pass the selected client ID back to the controller. How to do it?
source
share