Solved

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

Posted on 2013-01-23
5
420 Views
Last Modified: 2013-12-13
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()
                     };
                     customerList.Add(item);
                 }
             }
             ViewData["CustomerID"] = customerList;

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

     @Html.DropDownList("CustomerID")

Everything is working just fine.

My question is how do you return the selected value from the drop down list to the  controller?
0
Comment
Question by:hums
5 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38809589
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.
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38814191
Well you would do it with ajax:

$('#CustomerID').change(function(){

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

$.ajax({
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

0
 

Accepted Solution

by:
hums earned 0 total points
ID: 38815075
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  
 [HttpPost]
     //  @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.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about why website design really matters in today's demanding market.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The viewer will learn how to count occurrences of each item in an array.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now