1
I’m having trouble loading a DROPDOWNLIST that is dynamically created. When selecting a CONTACT FORM, I need the dropdownlist CONTACT TYPE to be loaded with the related options. The problem is that I am creating the fields dynamically and when I need to call them, I use the id attribute of both... In the dropdownlist CONTACT FORM, I created a change event that takes the value of the field and makes a query in the database to load the second dropdownlist CONTACT FORM. The problem is that the dropdownlist field CONTACT FORM will be repeated several times and the id index will be incremented... I needed, in the change event, to save the dropdownlist id CONTACT FORM to load correctly when necessary... Or make another mareira (I accept suggestions). Someone knows how to help me?
Big hug to all!
HTML:
<div class="col-md-3">
<label asp-for="PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.PessoaFisicaFormaContato" class="control-label lb-pf-forma-contato">Forma de Contato</label>
<select asp-for="PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.PessoaFisicaFormaContato" asp-items="@Model.PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.FormasContatos" data-id="@Model.PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.PessoaFisicaFormaContato" title="Selecione uma opção" class="form-control show-tick show-menu-arrow sel-pf-forma-contato"><option value=""></option></select>
<span asp-validation-for="PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.PessoaFisicaFormaContato" class="text-danger val-pf-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 asp-for="PessoaFisicaViewModel.PessoasFisicasContatosViewModel[i].PessoaFisicaFormaContatoTipoViewModel.ContatoTipo" asp-items="@Model.PessoaFisicaViewModel.PessoasFisicasContatosTipos" 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>
IDS generated:
CONTACT FORM - id="Personal Physiphysicsmodel_personal Physicscontact Uscontact Usviewmodel_0__personal Physicscontact Uspoviewmodel_personal Physicscontact Us"
TYPE OF CONTACT id="Personal Physicscaviewmodel_person"
JAVASCRIPT:
$(document).ready(function () {
$('#div-contatos').on("change", ".sel-pf-forma-contato", function (e) {
//var teste = $(this).attr("data-id");
//alert(teste);
$.ajax({
url: "/pessoa-fisica-gerenciar/getContatoTipo",
type: "POST",
data: { pessoaFisicaFormaContato: $(this).val() },
traditional: true,
success: function (result) {
var select = $("#contato-tipo-select");// Aqui carrega o dropdownlist TIPO DE CONTATO
alert(select);
select.empty();
select.append($('<option/>', {
value: "",
text: ""
}));
$.each(result, function () {
$.each(this, function (i, item) {
select.append($('<option/>', {
value: item.value,
text: item.text.toUpperCase()
}));
});
//select.append($('<option/>', {
// value: itemData.Value,
// text: itemData.Text
//}));
});
},
error: function () {
alert("Something went wrong call the police");
}
});
});
});
You’d get the id with
$(this).attr("id")
(jQuery) orthis.id
(JS pure).– Sam
That’s the problem @Sam!!! If I have two or more dropdownlists, the ids will have index ([0], [1], [2]...)
– Master JR
Ex: id="Personfisicaviewmodel_person"
– Master JR
You wanted to get the id without the numbers?
– Sam
Take a look at the print where I marked with red arrows... When selecting an option in the contact form dropdown, I have to record somehow the id of the dropdown "contact type" that is in the same Row... If I have more than one Row there is no way to load the records into the dropdown...
– Master JR
The problem is that as I create a new Row, the id will change and being generated an index for each field... Look at the name of the ids in print..
– Master JR
From what I understood then is that you do not know which dropdown to fill when changing the "contact form" because the id of the "contact type" changes with each line? Each pair of dropdown is in one
.row
?– Sam
That’s right!!! I thought about saving the id of the second combobox in the change event of the first combo.... and then using it.. but I don’t know how to do that...
– Master JR
Let’s go continue this discussion in chat.
– Sam