0

So I have a json that calls a JsonResult

The class:

public class Client{
    public int Id {get;set;}
    public string Name {get;set;}
}

The action being called:

public JsonResult Index(int Id)
        {
      var a = context.Clients.ToList();
      return Json(a, JsonRequestBehavior.AllowGet);
}

this is the call

<script type="text/javascript">
    $(document).ready(function () {
        var link;
        $('a.client-list').click(function () {
            link= $(this);
            $.ajax({
                url: '/client/index?Id=4455',
                dataType: 'json',
                success: function (data) {
                    $.each(data, function (id, val) {
                        alert(id.toString() + ' ' + val.toString());
                    });
                }
            });
            return false;
        });
    });
    </script>

So my problem is, I know that it returns something cuz it loops through the alert that I put in. but the value that pops out is this

0 [object Object]
1 [object Object]

I'm not sure why it's not reading it properly. The values queried btw are

1 TestCompany1
2 TestCompany2

Am I missing something on the jquery??

5
  • Are you sure that the TestCompany1 and TestCompany2 values you're getting back are strings? If they're any other kind of object (except for ones that can be converted to strings), your .toString() isn't gonna know how to convert it to a string. Commented Aug 16, 2012 at 21:41
  • 1
    Wouldn't each val be a Client object? So maybe it would be val.Name? Commented Aug 16, 2012 at 21:44
  • well, the Id is an int, and the Name is of string property. I thought I can just pass an IEnumerable or a var to the json and the getjson will take care of it? no? @MrOBrian I also tried that and still the same.. Commented Aug 16, 2012 at 22:00
  • you may need to analyse the structure of the returned json. Also, I'm not sure if it's causing confusion, but the id in $.each(data, function (id, val) is not Client.Id, but rather the array index. It may be easier (and less overhead) to use a standard javascript for loop there instead of $.each Commented Aug 16, 2012 at 22:12
  • something like this?? stackoverflow.com/questions/5954633/… Commented Aug 16, 2012 at 22:20

1 Answer 1

4

Update your $.each like so:

$.each( data, function (index, client ) {
    alert( client.Id + ' ' + client.Name );
});

You are returning a collection (Array) of the Client type. The $.each function will provide the index of the array and the item in the array associated with the index. In this case, each item in the array will be a Client Object. More information on $.each here: http://api.jquery.com/jQuery.each/

Fore more details when debugging JavaScript, try using the console:

$.each(data, function ( index, client ) {
    console.log( client );
});

Just hit F12 in your browser (PC) and select the console tab. You will be able to see more detail about the Object including its properties.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.