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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…