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??
TestCompany1andTestCompany2values 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.valbe aClientobject? So maybe it would beval.Name?idin$.each(data, function (id, val)is notClient.Id, but rather the array index. It may be easier (and less overhead) to use a standard javascript for loop there instead of$.each