Search code examples
asp.net-mvcpostbackdata-entry

asp.net MVC handling dependencies between input controls?


I have an application that has two dependent dropdown lists, where if the user selects a value in list box A, it updates the available set of inputs in list box B. Such as make/model of a car. when the user selects the manufacturer, the list of models would update accordingly.

In winforms, this would just be handled in the autopost back event. What technique/approach should I take in asp.net MVC? is done through AJAX ? just trying to get up to speed on MVC and looking to build strategies to handle common tasks I am asked to handle at work.

I wanted to thank everyone who contributed to answering this post.


Solution

  • I'd do this through ajax. If you have these controls:

    <select id="makes" /> and <select id="models" />

    then you can do this with jquery:

    $().ready(function() {
       $("select#makes").change(function() {
            var make = this.value;          
            $.getJSON('models/list?make=' + make, function(data) {
                  //load 2nd dropdown with result
            })
       });
    });
    

    Then you'd just need an action on the ModelsController called List() that returns a JSON data structure.