Solved

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

Posted on 2013-01-23
5
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

736 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