Getting a selectd item back from a MVC dropdown list view ?

I have a drop down list that I populate in my MVC controller with the following code.

var allCustomers = _customerService.GetCustomersByCustomerRoleId(3, false);

             var customerList = new List<SelectListItem>();
             foreach (var cr in allCustomers)
                 if (!cr.IsAdmin())
                     var item = new SelectListItem()

                         Text = cr.Email,
                         Value = cr.Id.ToString()
             ViewData["CustomerID"] = customerList;

In the cshtml file the drop down list is populated with the following code.


Everything is working just fine.

My question is how do you return the selected value from the drop down list to the  controller?
Who is Participating?
humsConnect With a Mentor Author Commented:
FYI - The framework returns the Value field in the form of a string to the controller in the  name you give to the dropdown list.

In my case  
     //  @Html.DropDownList("CustomerID")
        public ActionResult POSCarts(string CustomerID)

the "Value" field from the SelectListItem was put into the "string CustomerID " field passed back to the ActionResult method.

That was all the info I needed to continue with the program. Kind of a quick and dirty way to get the information from a dropdown list.

Thanks for your help.
Meir RivkinFull stack Software EngineerCommented:
why do u wanna return it to the controller?
if you use MVC, you should have some kind of service call from your client side (ajax maybe) passing relevant data to the service, via json format or xml or whatever.
Well you would do it with ajax:


var th = $(this):
var itemval = th.children('option:selected').val();

url: 'controller_url',
type: 'either post or get' // eg. : type:'post',
data: 'data='+itemval,
success: function(data){
// do something with server response here.


Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.