0
I have this Javascript that arrow the values to the Dropdown:
$('#Estado').change(function () {
var id = $(Estado).val();
$(function buscarCidade() {
//alert(id);
var url = '@Url.Action("SelecaoCidade", "Chamada")';//url do controller que passará a informação
$.post(url, { id: id },
function (data) {//Caso o retorno dê certo
//window.alert(data.length);
$('#Cidade').children().remove();
if (data.length > 0) {
$.each(data, function (index, value) {
$('#Cidade').append('<option value="' + value.Id + '">' + value.Nome + '</option>');
});
} else {
var novaOpcao = $('<option value=""></option>');
$('#Cidade').append(newOption);
}//fim if else
});//fim do post
});//BuscarCidade
});//Estado
I need to get the name and ID of the cities that were entered, and I tried to use these code snippets to get the id, but it didn’t roll:
var idCid = City.value;
var idCid = $(City). val();
var idCid = City.index;
$("#Status"). attr("id"); See if you can do so.
– Douglas Garrido
I believe that simply by:
var idCid = $('#Cidade').attr("id");
and `var name = $('#City'). attr("name");– BrTkCa
Is there an error? What is the value of the "date" parameter? Is it really an object that contains the "Id" and "Name" properties? Make a
console.log(data);
and post the result here.– Filipe Moraes
Douglasgarrido and Lucascosta attr("id") showed me only 'City' (not the name of the city), but attr("name") gave Undefined
– Fabio Souza
@Filipemoraes I don’t know if it’s right, but I got this data: no element found abort:1:1 The getPreventDefault() method should no longer be used. Instead, use defaultPrevented. browserLink:37:40278 Referenceerror: data is not defined[Learn More] Vote:226:33
– Fabio Souza
This is Json’s answer: [{"CidadeID":1,"Nome":"Aparecida","EstadoID":25,"Estado":null,"Locais":null},{"CidadeID":2,"Nome":"Guaratinguet
á","EstadoID":25,"Estado":null,"Locais":null},{"CidadeID":3,"Nome":"Roseira","EstadoID":25,"Estado":null
,"Locais":null},{"CidadeID":4,"Nome":"Lorena","EstadoID":25,"State":null,"Locations":null},{"Cidadeid" :5,"Name":"Taubaté","Estadoid":25,"State":null,"Locations":null},{"Cidadeid":6,"Name":"Caçapava","Estadoid" :25,"State":null,"Locations":null,"}]
– Fabio Souza
So Fábio. The json does not have the property "Id" but "Cidadeid". In its code, where you have value.Id, change it to value.Cidadeid
– Filipe Moraes
Ball Show @Filipemoraes Valeu
– Fabio Souza