Return with multiple json with jquery . net

Asked

Viewed 160 times

1

I need to return to ajax a json, or rather several json, being city,neighborhood,state,parents, each being a json in the same method, however as not possible to make several return put the list of obj above in a list of list.

        List < List<String> > final = new List < List<String> >();
        final.Add(cidade);
        final.Add(bairro);
        final.Add(estado);
        final.Add(pais);

just below return the json from the final list

        return Json(final, JsonRequestBehavior.AllowGet);

however when I will display the data in ajax, when accessing the given position[1] or 2 ..., it takes as an entire list and not as a json, I think it returned the json only the positions 0,1,2... and not what has inside it, how to do to return this data as json

<script>
$(document).ready(function () {
     cidade.change(function () {
        $(function () {
            $('#loading').html('<img  
            $.ajax({
                dataType: "json",
                type: "GET",
                url: "GetDados",
                data: { Cidade: cidade.val() },
                success: function (dados) 
                $(dados).each(function (i) {
                   alert(dados[1]);
                    });
                 }
            });
        });
    });
})

</script>
  • 1

    Show the java scrip you are using to view?

1 answer

2


controller:

var result = new { cidade, bairro, estado, pais };
return Json (result, JsonRequestBehavior.AllowGet);

"result" is a anonymous object

javascript:

<script>
$(document).ready(function () {
     cidade.change(function () {
        $(function () {
        $('#loading').html('<img  
        $.ajax({
            dataType: "json",
            type: "GET",
            url: "GetDados",
            data: { Cidade: cidade.val() },
            success: function (dados) {
               alert(dados.cidade);
             }
        });
    });
});
</script>

Browser other questions tagged

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