Update DropDrownList in ASP.NET MVC when user selects un option in another DropDrownList

Does anyone know how to copy combos with values ​​depending on custom seleccion, I need combos, country and region. When the user selects contry, I need to go to the database and get the region in the country. how to do it if you use AJAX positively.

Thanks.

+3
source share
3 answers

Do you need to use a specific libarary Ajax?

The Ajax.Net Control has a Cascading Dropdown pre-created for this function.


MVC, Ajax Control Toolkit . jQuery, , , Ajax , MS MVC.

RESTful ( , ), json- jQuery :

:

<select id="opt1">
  <option value='1'>Option 1</option>
  <option value='2'>Option 2</option>
</select>

<select id="opt2" disabled></select>

, "id" json, , :

[
  { name: 'option1', value: '1'},
  { name: 'option2', value: '2'}
]

javascript :

$(function() {
  $('select#opt1').change(function(){
    $.getJSON('/data/myservice',{id: this.value},
      function(response) {
        var options = '';
        for (var i = 0; i < response.length; i++) {
          options += "<option value='" + response[i].value + "'>"
                  +  response[i].name + "</option>";
        }
        $('select#opt2').removeAttr('disabled').html(options);
      });
  });
});

, ; jQuery , :

$(function(){}) $(document).ready(function(){}), , .

$('select#opt1').change(function(){}) <select> 'opt1' , .

$.getJSON() Ajax . JSON JSON, $.getJSON . - URL- , - /, , - .

+8

jQuery MVC JsonResult . - , . JsonResult , jQuery, , .

jQuery MVC JsonResult

+1

I also recommend jQuery, Json actions are useful if you go and get the data asynchronously. The end result will be a coordinated build of the combo box, there are many examples of how to do this in jQuery, this is pretty good with live demos and code. Hope this helps you.

0
source

All Articles