[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-01-23
5
Medium Priority
?
426 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:Meir Rivkin
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

656 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