0

I want to display a json string returned in the result of an ajax call in the from of a table. My ajax code is :

var table = $('#PartnersTable');
        $.ajax({
            type: "GET",
            url: '@Url.Action("Get","Home")',
            dataType: 'json'
        }).done(function (data) {
            // Code here
        });

I have tried many solution , that are on stackoverflow but they didn't work. Thanks in advance

This is my action code :

public ActionResult Get()
        {
            Home h = new Home();
            return Json(h.get(), JsonRequestBehavior.AllowGet);
        }
Hammad Shahid
  • 1,996
  • 5
  • 26
  • 57
  • You to iterate through each record in data and create required TR TD for it... or else you can use jqGrid plugin as well – K D Jun 26 '13 at 11:21
  • How do I access the record ? data.ID , data.Name ? – Hammad Shahid Jun 26 '13 at 11:21
  • 1
    please post the object which you are sending from the Home/Get Action... Or else please post the Action method code itself... if you are sending a single object then it will have data.PROPERTYNAME (data.ID) BUT if you are sending array of object then it will be data[0].ID and so on.... – K D Jun 26 '13 at 11:23
  • I used alert(data[0].ID) , but it is not displaying anything – Hammad Shahid Jun 26 '13 at 11:29
  • What this h.get() method returns ??/ – K D Jun 26 '13 at 11:29
  • It returns "undefined" , I have placed my action code above , you can see it – Hammad Shahid Jun 26 '13 at 11:32
  • I suggest you use `Fiddler`, or the dev tools in your browser, to capture what is being returned from the action method. That way you know precisely what data is coming back. – Jason Evans Jun 26 '13 at 11:36
  • It returns this string {"Homes":[{"ID":2,"Name":"Hammad"},{"ID":3,"Name":"dsda"} – Hammad Shahid Jun 26 '13 at 11:38

1 Answers1

0

considering that your h.get() method will returns object which has following properties...

{"Homes"
      :[
         {"ID":2,"Name":"Hammad"},
         {"ID":3,"Name":"dsda"} 
        ]
}

These values will get accessed in following manner

data.Homes[0].ID
data.Homes[0].Name
K D
  • 5,515
  • 1
  • 19
  • 32