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. (Last updated on : November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

From DropdownList, how do you get ID of the selected item in controller?

Thread ID:

Created:

Updated:

Platform:

Replies:

132508 Sep 5,2017 10:51 PM UTC Sep 7,2017 04:31 AM UTC ASP.NET MVC 3
loading
Tags: DropDownList
Andrew Jang
Asked On September 5, 2017 10:51 PM UTC

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



Keerthana Rajendran [Syncfusion]
Replied On 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   
 


Andrew Jang
Replied On September 6, 2017 06:39 PM UTC

This works, thank you very much!


Keerthana Rajendran [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;