开发者

Asp.net MVC Ajax iteration

开发者 https://www.devze.com 2023-03-18 15:08 出处:网络
I am returning an object to my view using $.getJSON.The object contains a number of Lists, that I need to iterate over.How can this be done?My 开发者_运维技巧code so far is:

I am returning an object to my view using $.getJSON. The object contains a number of Lists, that I need to iterate over. How can this be done? My 开发者_运维技巧code so far is:

$.getJSON("/Home/GetData",
        function (data) {
            $.each(data, function (index) {
                //access data here for each list in object?
            });
        });

Thanks.


If I understand what you're asking here, you're getting back JSON object of this form:

// result
{
    people: [{...}, {...}, ... , {...}],
    places: [{...}, {...}, ... , {...}],
    ...
}

You should first iterate over your returned JSON object and then iterate over each list's items individually. Something along these lines:

$.getJSON("/Home/GetData", function (data) {
    // iterate over lists in an object
    for(var list in data)
    {
        // list variable holds the name of the list (ie. "people")
        // iterate over list items
        $.each(data[list], function (index) {
            // do something with this list item
        });
    }
});


When using $.each, the signature is:

$.each(data, function (index, item) {
    //access data here for each list in object?
});

So the item can be accessed directly. Also, I think a list should be pushed up as an array, so you can have any number of $.each statements as you need. The next inner array would be available via:

$.each(item.SubList, function(si, sitem) {

});

If you post the data structure pushed, we could help more. Also, the object you are returning, is it an anonymous class or strongly-typed? Are you returning a JsonResult?

HTH.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号