Solved

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

Posted on 2013-01-23
5
417 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DNS, website, godaddy 6 41
.net VBA word safemode 1 23
Video on my site 4 26
Code works but how can I download the file? 20 44
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
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 get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

744 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

17 Experts available now in Live!

Get 1:1 Help Now