Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with getting selected OBJECT from dropdownlist

I have a problem with getting selected objectfrom my list.

I bind collection of users to dropdownlist by:

 ddlContractors.DataSource = service.GetAllUsers();
                ddlContractors.DataTextField = "Name";
                ddlContractors.DataValueField = "Id";
                ddlContractors.DataBind();

It's working. But when I try getting selected object by:

var user = (User)ddlContractors.SelectedItem;

I get:

    (User)ddlContractors.SelectedItem   Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'GWDSite.GWDService.User'

How can I get object user from dropdownlist ? I can change type of my list control if it is necessary

like image 705
user278618 Avatar asked Dec 13 '25 18:12

user278618


1 Answers

The value field in the dropdown list is the field "Id" not the User object. so 'SelectedItem' is returning the "Id" value -- not the object. You can use that Id to lookup the User object (from session or cache or wherever you can keep it)

like image 186
Jim Ecker Avatar answered Dec 16 '25 23:12

Jim Ecker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!