I am making an application that deals with vehicles. I need two DropDownLists:
- Makes: All Vehicle Makes
- Models: Models that belong to the selected value of the Make DropDownList
How is this done in MVC2?
My Idea: Do I use an ajax call when my first list is selected and then pull back the Models to bind to the Model DDL? How would model binding come into play that way?
UPDATE I posted what I ended up doing as an answer. It is super simple and works great.
You can use a get too if you feel so inclined, but you have to specify that you want to like so... return Json(citiesList, JsonRequestBehavior.AllowGet);
The simpliest way is to use jQuery "cascade" plugin. http://plugins.jquery.com/project/cascade (look at the demo page there).
If you want to resolve value using ajax, it also helps you and it eliminates a lot of code from the previous answer, so you can concentrate on your logic :)
You can find a lot of examples in google, but you end up with just the following script:
Here's a nice way to do it :
Suppose that we have two drop lists, country and city ,, the city drop down is disabled by default and when a country is selected the following happens :
Credits for the original code goes to King Wilder from MVC Central. This example is a simplified version that was extracted from his code in the Golf Tracker Series.
HTML
JavaScript
Action Method
Important Notes:
1. The
JsonSelectObject
help make things easier when converting the results to an option tag as it will be used in the javascriptloadSelect
method below. it's basically a class with the two properties value and caption :2. The function
loadSelect
is a helper method that takes a list of json objects originally of typeJsonSelectObject
, converts it to a list of options to be injected in calling drop down list. it's a cool trick from the "jQuery In Action" book as referenced in the original code, it's included in ajquery.jqia.selects.js
file that you will need to reference. Here's the code in that js file :This method might be complex ,,, but at the end you will have a clean & compact code that you can use everywhere else.
I hope this was helpful ,,,
Update
Using POST instead of GET in the AJAX call
You can replace the
$.getJSON
call with the following code to make the ajax call using POST instead of GET.also remember to change your Action method to accept POST requests by changing the [HttpGet] annotation with [HttpPost] and remove the
JsonRequestBehavior.AllowGet
when returning the result in the Action Method.Important Note
Note that we are using the value of the selected item rather than the name. for example if the user selected the following option.
then "US" is sent to the Action method and not "United States"
Update 2: Accessing the Selected Values In The Controller
suppose that you have the following two properties in your
Vehicle
viewmodel:and you name your select elements with the same name as your ViewModel properties.
Then the selected values will be automatically Bound to your ViewModel and you can access them directly in you Action Method.
This will work if the page is Strongly Typed to that ViewModel.
Note: For the first list ( The Makes List ) you can create a MakersList of type SelectList in your ViewModel and use the HTML helper to automatically populate your Makers Drop Down with the list in your ViewModel. the code will look something like this :
In that case the generated name for this select will be also "Maker" (the name of the property in the ViewModel).
I hope this is the answer you are looking for.
This is what I ended up doing... Didn't need additional plugins / 1000 Lines of code...
The Html
The JQuery
The Action