user1006544
user1006544

Reputation: 1524

How to parse System.Collections.Generic.List in jquery?

I am using a jquery to call a collection Suppose this is my collection

        PageBL pageBL = new PageBL();
        List<Page> pageList = pageBL.GetCategoryPageList(categoryID);
        return pageList;

I am getting this list in jquery,

$.get("/Home/GetActionMethod/" + id, { CategoryID: id }, function (data) {

            });

Now can any one tell me how this data is parsed so that I get the required result.

Upvotes: 0

Views: 6787

Answers (2)

Snake Eyes
Snake Eyes

Reputation: 16764

Please read my topic already opened, it might help you Return a JSon array to $.ajax from ActionResult type method in MVC 3

or

var data=null;
$.ajax({
         url: '/Home/GetCategoryPageList/',
         dataType: 'json',
         contentType: 'application/json; charset=utf-8',
         data: {CategoryID: id}
         success: function (msg) {
            data = msg;
         },
         complete: function () {
           //do something with data
           $.each(data, function(index, value)
           { 
             $("#div1").append(value.PageName); //or other property from generic list
            } 
         }
      });

I'm assuming that you have a generic list which contains jgauffin structure (PageName, Title, SomeOtherProp, etc.)

in your controller

PageBL pageBL = new PageBL();
        List<Page> pageList = pageBL.GetCategoryPageList(categoryID);
        return this.Json( new {
         msg = pageList
      });

Upvotes: 0

jgauffin
jgauffin

Reputation: 101166

You should use return Json(pageList); in your controller action,

The format will depend on the Page layout. But something like:

[
    {"PageName": "Title", "SomeOtherProp": "Value"}, 
    {"PageName": "Some other page", "SomeOtherProp": "Value2"}
]

PageName and SomeOtherProp correspond to properties in the Page class

To traverse the information and handle it you can just do:

$.get("/Home/GetCategoryPageList/" + id, { CategoryID: id }, function (data) {
    $.each(data, function(item) {
        alert('Property from an item: ' + item.PageName);
    });
});

Upvotes: 1

Related Questions