We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

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



3 Replies

KR Keerthana Rajendran Syncfusion Team September 6, 2017 11:57 AM UTC

Hi Andrew,   
   
Thank you for using Syncfusion products.   
   
We checked your code. You have assigned designation as value for DropDownList so you received the selected item’s value during postback. If you want to get employeeId during postback, we suggest to set the employeeId as value field of DropDownList as shown below   
   
@Html.EJ().DropDownList("selectCar").Datasource((IEnumerable<Employee>)ViewBag.datasource).DropDownListFields(df => df.ID("employeeID").Text("Name").Value("employeeID")).WatermarkText("Select a Student").Width("100%")   
   
We have attached a sample for your reference.   
   
   
   
Regards,   
Keerthana   
 



AJ Andrew Jang September 6, 2017 06:39 PM UTC

This works, thank you very much!



KR Keerthana Rajendran Syncfusion Team September 7, 2017 04:31 AM UTC

Hi Andrew, 
 
Thank you for your update. We are happy to hear that our suggestion helped to achieve your requirement. Please get back to us if you require any further assistance on this. We will be happy to assist you 
 
Regards, 
Keerthana. 


Loader.
Up arrow icon