Fill a select with callback $getJson Asp.Net / Jquery

Asked

Viewed 404 times

2

What I need to do is this, I have a select with the states, when the person changes that state I need to fill another select with the cities.

This is the state select :

                            <div class="input-group">

                                <select class="selectpicker dropdown" data-live-search="true" id="selectEstado">

                                    @foreach (Estado estado in Model.Cidade.Estados)
                                    {
                                        if (estado.EstadoId == Model.Cidade.EstadoId)
                                        {
                                            <option selected value="@estado.EstadoId">@estado.Nome</option>
                                        }
                                        else
                                        {
                                            <option value="@estado.EstadoId">@estado.Nome</option>
                                        }

                                    }

                                </select>

                            </div>
                        </div>

The function I’m trying to do is this: :

    $("#selectEstado").change(function () {
    var lang = $("select option:selected").val();
    $("#language").text(lang);

    $.getJSON("/Cidade/GetCidadePorEstado?EstadoId=" + lang, null, function (cb) {
        $("#selectCidade").html(cb.CidadeNome);
        console.log(cb);
    });
});

He is entering the controller of C# and returning a model with all the cities, but I do not know how to proceed later, I try to verify the value of cb but I am not getting, I have little knowledge in this area if someone can give me a strength.

From now on I thank you

EDIT So is now my function

    $("#selectEstado").change(function () {
    var EstadoId = $("select option:selected").val();
    $("#language").text(EstadoId);

    $.getJSON("/Cidade/GetCidadePorEstado?EstadoId=" + EstadoId, null, function (cb) {      
        $("#selectCidadeNovo").append('<option value="' + cb.CidadeNome + '">' + cb.CidadeNome + '</option>');
        console.log(cb);
    });
});

the log this showing an object CidadeNome:Array[141] which are the Names of the Cities that I received from cb but does not change the names of the Cities.

By default the two select already comes with values because this screen and edit client, then this client already has a city, so when changing the status I need to reload the select of the city with the new callback

So and the select that needs to be changed:

 <div class="form-group">

                            <div class="input-group">

                                <select class="selectpicker dropdown" data-live-search="true" id="selectCidade">

                                    @foreach (Cidade cidade in Model.Cidade.Cidades)
                                    {
                                        if (cidade.CidadeId == Model.Cidade.CidadeId)
                                        {
                                            <option selected value="@cidade.CidadeId">@cidade.Nome</option>
                                        }
                                        else
                                        {
                                            <option value="@cidade.CidadeId">@cidade.Nome</option>
                                        }

                                    }

                                </select>

                            </div>
                        </div>
  • In my case the problem and add to a select with the selectpicker class , different from the question you marked.

1 answer

2


You can iterate the result of cities received in cb, and instead of using html use append to add a option per city. Example (assuming that the return is an array of cities):

var cb = ["São Paulo","Campinas", "Sorocaba"];
cb.forEach(function(cidade) {
  $("#selectCidade").append('<option value="'+cidade+'">'+cidade+'</option>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selectCidade">
</select>

EDITED:

$.getJSON("/Cidade/GetCidadePorEstado?EstadoId=" + EstadoId, null, function (cb) {      
    $("#selectCidadeNovo").clear();
    cb.CidadeNome.forEach(function(cidade){
        $("#selectCidadeNovo").append('<option value="' + cidade + '">' + cidade + '</option>');
    });
 $('.selectpicker').selectpicker('refresh'); //Recarrega o selectpicker com os novos dados
});
  • I will try here, I think this should resolve, just a doubt and I would like to put the value of options the same name of the city, I saw something like $("#selectCidade").append(&#xA;$("<option></option>")&#xA; .text(cidade)&#xA; .val(cidade));

  • I edited with the value on option @Williamcézar

  • 1

    Thanks man I’ll try here, soon I’ll be back

  • Guy didn’t work out, I’ll do an Edit with the changes and other doubts

  • Try leaving the code snippet as I edited @Williamcézar

  • OK I’ll try now

  • Not changing the Select, nothing I tried changes the select, the last function you passed to see that goes giving append in each city, but does not change anything in select, remembering that I am using the selectpicker.

  • Any error in console?

  • 1

    Not one , I took the datepicker and it worked, I will see the documentation to see if it has something related, but its function was right, thanks for the help.

  • I will find the solution to the selectpicker and add your answer

  • Please make yourself at home.

  • Friend yesterday I found the solution for datepicker, need to reload it so that appear the new data : $('.selectpicker'). selectpicker('refresh'); I edited your responsta, thank you very much.

  • Cool @Williamcézar! I learned from this one too, tmj

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.