Reputation: 20179
I have an object with 2 ArrayList properties.
public class TestDTO
{
public ArrayList Test1 { get; set; }
public ArrayList Test2 { get; set; }
}
I am returning the the object as JSON in my JsonResult Action. The SUCCESS from my AJAX call looks like the following but it does not appear to be working. What do I need to do to access the serialized array from the object?
success: function(data) {
var counter = 1;
jQuery.each(data.Test1, function() {
$("#DataFields" + counter).val(this);
counter++;
});
},
Upvotes: 1
Views: 1817
Reputation: 6706
One way to see what the result looks like is to use Firebug in Mozilla. Or the stand-alone application Fiddler.
I think that the class wraps the whole object, so it should look something similar to this:
TestDTO {
Test1 [
"value", "value 1", "value 2"
],
Test2 [
"value", "value 1", "value 2"
]
}
If you'd like to access Test1 you'd write TestDTO.Test1. If you want the first value in Test1 you'd write TestDTO.Test1[0] and so on...
One way to get more control of the output is to use LINQ and select new:
select new {
MyVariable = MyValue,
AnotherVariable = AnotherValue
}
Now you know that "MyVariable" will be the JSON variable name as well.
Upvotes: 3
Reputation: 32169
Maybe the following is useful?:
success: function(data) {
$.each(data.Test1, function(i, item) {
$('#DataFields' + i).val(item);
});
}
Upvotes: 2
Reputation: 24388
When I get in to situations like this, I use firebug and break somewhere inside the success method. Then check out the actual structure of data and I'll bet it will be obvious.
Upvotes: 1