Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my Model:

C#
public class TeamRosterModel
    {
        public SelectList ManageList { get; set; }
        public string Command { get; set; }
    }
    public class Employee
    {
        public string FromEmpID { get; set; }
        public string EmployeeName { get; set; }
    }



This is my view:
C#
@Html.DropDownList("Employee", new SelectList(Model.ManageList, "Value", "Text", Model.ManageList.SelectedValue))


This is my controller to set the drop down list:

C#
List<Employee> objEmployee = new List<Employee>();
            for (int i = 0; i < dsManageFor.Rows.Count; i++)
            {
                string d1 = dsManageFor.Rows[i][0].ToString();
                string d2 = dsManageFor.Rows[i][1].ToString();
                objEmployee.Add(new Employee { FromEmpID = d1, EmployeeName = d2 });
            }
            sqlConn.Close();
            objTeamModel.ManageList = new SelectList(objEmployee, "FromEmpID", "EmployeeName", 0);


This is my controller code to retrieve selected value:
C#
string value = objTeamModel.ManageList.SelectedValue;



I am able to set the values but not able to retrieve the selected value from the drop down... Please help
Posted
Updated 18-Jun-13 4:46am
v2

1 solution

You should use DropDownListFor, that will let you assign the collection AND a variable to store the selected value.

Otherwise you have to pull it out of the Request. You might be able to create a variable and have it auto populate, but you're better off letting the framework know ahead of time what you want. For all that, I tend to use AJAX all the time, so I end up creating an AJAX post that defines exactly what variables are posted back, and put them on the method being called.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900