Problems loading Dropdownlist in cascade - Asp.net Core MVC

Asked

Viewed 223 times

2

I have two combobox in which the second has a cascade load that is, when I select something in the first, a query is made in the database and the data must fill in the second... My controller seems to be returning a json array correctly, but I think when it comes to loop (JS block) I am not able to properly load the second bombobox (id="contactType")... Someone knows how to help me?

inserir a descrição da imagem aqui

Controller:

[HttpPost]
[Route("pessoa-fisica-gerenciar/getContatoTipo")]
public IActionResult getContatoTipo(PessoaFisicaFormaContato pessoaFisicaFormaContato)
{
    var pessoasFisicasFormasContatos = new SelectList(_pessoaFisicaFormaContatoTipoAppService.GetAllByPessoaFisicaContato(pessoaFisicaFormaContato), "Id", "ContatoTipo", 0);
    return Json(new
    {
        data = pessoasFisicasFormasContatos
    });
}

Dropdowns:

<div class="col-md-3">
    <label asp-for="PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.PessoaFisicaFormaContato" class="control-label lb-pessoa-fisica-forma-contato">Forma de Contato</label>
    <select  id="pessoaFisicaFormaContatoTipo" asp-for="PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.PessoaFisicaFormaContato" asp-items="@Model.PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.FormasContatos" data-plugin="selectpicker" title="Selecione uma opção" class="form-control show-tick show-menu-arrow sel-pessoa-fisica-forma-contato"></select>
    <span asp-validation-for="PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.PessoaFisicaFormaContato" class="text-danger val-pessoa-fisica-forma-contato"></span>
</div>
<div class="col-md-3">
    <label asp-for="PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.ContatoTipo" class="control-label lb-contato-tipo">Tipo de Contato</label>
    <select id="contatoTipo" asp-for="PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.ContatoTipo" asp-items="@Model.PessoaFisicaViewModel.PessoasFisicasContatosTipos" data-plugin="selectpicker" title="Selecione uma opção" class="form-control show-tick show-menu-arrow sel-contato-tipo"></select>
    <span asp-validation-for="PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.ContatoTipo" class="text-danger val-contato-tipo"></span>
</div>

JS:

<script>
    $(document).ready(function () {
        $("#pessoaFisicaFormaContatoTipo").on("change", function () {
            $list = $("#contatoTipo");
            $.ajax({
                url: "/pessoa-fisica-gerenciar/getContatoTipo",
                type: "POST",
                data: { pessoaFisicaFormaContato: $("#pessoaFisicaFormaContatoTipo").val() },
                traditional: true,
                success: function (result) {
                    $list.empty();
                    $.each(result, function (i, item) {
                        $list.append('<option value="' + item["Id"] + '"> ' + item["ContatoTipo"] + ' </option>');
                    });
                    console.log(result);
                },
                error: function () {
                    alert("Something went wrong call the police");
                }
            });
        });
    });
</script>
  • Isn’t the return "value" and "text"? here shouldn’t be $list.append('<option value="' + item["value"] + '"> ' + item["text"] + ' </option>');

  • Hello @Barbetta! I tried to do according to your suggestion, even so, after loading, when I inspect the field, it looks like this: <option value="Undefined"> Undefined </option>

  • item.text tbm?

  • Equal....... :(

  • is.. there has to debug rsrs

  • There is a problem with the $.each, because it is passing only once.... it ends up passing at once and can not pick up line by line.... I just don’t know how to adjust it @Barbetta

Show 1 more comment

1 answer

2


tries to change his success:

success: function (data) {
            var itens = "<option>Selecione Contato</option>";
            $.each(data, function (i) {
                itens += "<option value= " + data.id + "> " + data.contatoTipo + " </option>";
            });

            $("#contatoTipo").html(itens);
        },

http://prntscr.com/mckobi

  • Thanks @Luks Indie :)

  • worked out? I just don’t know how to format text yet on this kkk platform

  • kkk Boy, I got it... But before you paste the fonts, you play on the notepadd++ and tab everything... If you give tab on all lines and paste here he understands that you have to select all gray the source... srsrs

  • Okay, I had a lot of trouble with Dropdownlist cascading, among other things, I didn’t know anything about Javascript, I’m learning a lot, it’s making my life a lot easier in the "little project" of study I’m doing (.NET MVC). Need help just call, if I can help I’ll help rsrs.

  • Thanks @Luks Indie :)

Browser other questions tagged

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