Cascade Dropdownlist MVC 4

Asked

Viewed 791 times

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 action by the browser and see the return.

  • tried to make a return Json(data, JsonRequest...) straightforward?

  • 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

  • @Rodrigodetomini has done the debug of this JS?

  • @ 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!

1 answer

2

Putting as a response the solution I indicated in the comments.

In the line of your return you do not need to create a new encapsulation, change the line:

return Json(new { Result = data }, JsonRequestBehavior.AllowGet);

To

return Json(data, JsonRequestBehavior.AllowGet);

Browser other questions tagged

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