1
The code:
$(function () {
$('#IdiomaOrigem').change(function () {
var data = $("#IdiomaOrigem").val();
var destino = $("#IdiomaDestino").val();
$.ajax({
url: '/Solicitacao/CarregarViewBags?IdiomaOrigem=' + data + '&IdiomaDestino' + destino,
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value='" + item.value + "'>" + item.text + "</option>";
});
$('#IdiomaDestino').html(items)
}
})
})
})
My problem:
I have 2 select, The second is dynamically populated based on the value chosen in the first.
I am sending the selected values via ajax to the controller.
The first is sending correctly, as seen in the photo but the second has not yet been chosen when it makes the ajax request, so it returns null.
What I have tried: I created a new function in . change of the second select to send the selected value and send a fetch to the mvc controller
The code with the second function was like this
$(function () {
$('#IdiomaOrigem').change(function () {
var data = $("#IdiomaOrigem").val();
$.ajax({
url: '/Solicitacao/CarregarViewBags?IdiomaOrigem=' + data,
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value='" + item.value + "'>" + item.text + "</option>";
});
$('#IdiomaDestino').html(items)
}
})
})
})
$(function () {
$("#IdiomaDestino").on("change", function (info) {
var idiomaDestino = $("idiomaDestino").text();
fetch(`/Solicitacao/CarregaViewBags?IdiomaDestino=${idiomaDestino}`);
})
})
But still does not send the selected value The mistake now is this:
I also tried to replace:
var languageDestino = $("languageDestino"). text();
for:
var languageDestino = $("languageDestino"). val();
and:
var idiomDestino = $("#itemDestino"). html(items);
Ps: the second select is being populated but does not send to the back end.
Add the codes directly to the question (not print).
– Boneco Sinforoso
added codes
– Alaexandre
the selects also
– user60252