- Home
- Forum
- ASP.NET MVC
- From DropdownList, how do you get ID of the selected item in controller?
From DropdownList, how do you get ID of the selected item in controller?
Hi there,
Let's say this is my model class
public class Employee
{
public string name { set; get; }
public string employeeID { set; get; }
public string designation { set; get; }
public Employee() { }
public Employee(string name, string id, string designation)
{
name = name;
employeeID = id;
designation = designation;
}
}And in code beheind, I fetch the data as follows:
public ActionResult DropdownlistFeatures()
{
List<Employee> data = new List<Employee>() { };
data.Add(new Employee("Nancy", "E11011", "Technical Writer"));
data.Add(new Employee("Angel", "E11012", "Professor"));
data.Add(new Employee("Daniah", "E11013", "Dancer"));
data.Add(new Employee("Jenifer", "E11014", "Beautician"));
data.Add(new Employee("Prince", "E11015", "Developer"));
var db = from result in data
select result;
ViewBag.dataSource = db.ToList();
return View();
}View:
@Html.EJ().DropDownList("selectCar").Datasource((IEnumerable<Employee>)ViewBag.datasource).DropDownListFields(df => df.ID("employeeID").Text("name").Value("designation")).Value("Developer").WatermarkText("Select an Employee").Width("100%")
In a controller, let's say I am doing something like this:
[HttpPost]
public Task<ActionReuslt> ControllerAction1(FormCollection form)
{
var ID = form["selectCar"];
....
}
When I Post and run into ControllerAction1, form["selectCar"] always returns dropdownlists' selected value (string designation).
However, I would like to get the selected ID(employeeID), not value.
How can I do this??
|
@Html.EJ().DropDownList("selectCar").Datasource((IEnumerable<Employee>)ViewBag.datasource).DropDownListFields(df => df.ID("employeeID").Text("Name").Value("employeeID")).WatermarkText("Select a Student").Width("100%") |
This works, thank you very much!
- 3 Replies
- 2 Participants
-
AJ Andrew Jang
- Sep 5, 2017 10:51 PM UTC
- Sep 7, 2017 04:31 AM UTC