Change The DropDownList list in the OnChange event

0

good morning

I have the following code to assign a list to my DropDownList from the controller

ViewBag.Ciudad = new SelectList(GetCiudadAll, "idCiudad", "NombreCiudad");

in the view

   @Html.DropDownList("Ciudad", (SelectList)ViewBag.Ciudad, "-- Seleccione Ciudad --", new { @class = "form-control", required = "required" })

but it is necessary that for certain types of Tax the list of cities be changed for this I realize this in the view

    <script type=“text/javascript”>
    $('#TipoImpuesto').change(function() {
               if ($('#TipoImpuesto').val() == ('CO2')) {
                    {
                      var CiudadesPaises = ['Mexico', 'Caracas', 'Bogota', 'Lima', 'Quito'];
                        @ViewBag.Ciudad=CiudadesPaises;
                    }
                }
            });
</script>

but the DropDownList does not change the list that you have to display, how or why can not the new list be displayed in my view?

    
asked by ger 13.12.2016 в 14:59
source

1 answer

0

In the code that you show you are mixing parts that run on the client and parts on the server, it does not work

If you want to change dynamically using client code the combo items you should use jquery, but the ViewBag has nothing to do, that only applies when you send data from the action to the view when it is rendered on the server

<script type=“text/javascript”>
    $('#TipoImpuesto').change(function() {
       if ($('#TipoImpuesto').val() == ('CO2')) {
            {
                $('#Ciudad').find('option').remove();

                var CiudadesPaises = ['Mexico', 'Caracas', 'Bogota', 'Lima', 'Quito'];

                $.each(CiudadesPaises, function( index, value ){
                    var option = $('<option value="'+value+'">'+value+'</option>');
                    $('#Ciudad').append(option);
                });

            }
        }
    });
</script>

As you can see the idea is to select the combo and use the append() to add options, but everything is done by means of javascript / jquery code

    
answered by 13.12.2016 / 15:19
source