3
View:
<asp:Content ID="Javascript" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript">
$(document).ready(function () {
$("#estados").change(function () {
var uf = listaCidade($(this).val());
});
});
//chamada ajax para a Action ListaCidade
//passando como parâmetro a Estado selecionado
function listaCidade(uf) {
// <%=Url.Action("listaCidade")%>
$.getJSON('/AdmPaginaBranca/listaCidade/' + uf, listaCidadeCallBack);
}
//função que irá ser chamada quando terminar
//a chamada ajax
function listaCidadeCallBack(json) {
//Limpar os itens que são maiores que 0
//Ou seja: não retirar o primeiro item
$("#cidades:gt(0)").remove();
$(json).each(function (id, nome) {
//adicionando as opções de acordo com o retorno
//alert(data);
$("#cidades").append("<option value='" + this.id + "'>" + this.nome + "</option>");
});
}
</script>
Controller:
public ActionResult listaCidade(int id)
{
cidadeEstadoDAO obj = new cidadeEstadoDAO();
var estado = obj.CarregarComboCidade(id);
var data = estado.Select(m => new { m.id, m.nome }).ToList();
return Json(new { Result = data }, JsonRequestBehavior.AllowGet);
}
I have the following problem: when I select the state in dropdownlist, he calls the method normally and makes the select at the bank (checked by "inspect item") however, at the dropdown of the city, it is coming with the value Undefined. When I put a alert in "id" or "name" appears Undefined also.
Some charitable soul can help me?
Check the name of the attributes. If you are wrong (even a lowercase letter in the wrong place). Try to access
actionby the browser and see the return.– Randrade
tried to make a
return Json(data, JsonRequest...)straightforward?– Maicon Carraro
So, I think the problem is not in the method but in the jQuery code, I checked the action through the browser and it’s bringing everything.. " Result: [{id: 1200013, name: "Acrelândia"}, {id: 1200054, name: "Assis Brasil"},... ]".. I believe the problem is time to drop the list. I forgot to mention.. i don’t know anything about javascript/jQuery kk, so I’ll ask for your patience
– Rodrigo Detomini
@Rodrigodetomini has done the debug of this JS?
– Leonel Sanches da Silva
@ Maicon Carraro thank you so much for the tip! that was the reason that was not returning from the controler!. I did straight as Oce spoke and it worked out that it’s a beauty!!! thank you so much to everyone who helped!
– Rodrigo Detomini