I have a problem, I cannot add custom objects to my List. However, if I add some predefined objects (strings, integers..) my code works just fine.
This is my class which is always returned on all ajax calls to this controller, it contains boolean Status and List of objects.
public class returnData
{
public bool status { get; set; }
public List<object> data { get; set; }
}
This is the class for the object I want to add to this list:
public class file
{
public string ID { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
The code where I use this:
returnData data = new returnData();
...
DriveFile file = JsonConvert.DeserializeObject<DriveFile>(Json);
file f = new file();
if (file.mimeType == "application/vnd.google-apps.folder")
{
f.Type = "folder";
f.ID = file.id;
f.Name = file.title;
data.data.Add(f);
}
The error I get has no useful info, as this code compiles and the error is in Chrome Console log when the ajax is executed.
Image of google chrome console
However, when I use this line of code
data.data.add("some string");
my function will work flawlessly and return Object which contains boolean Status and List of objects (strings, integers..)
So back to my question, how do I add my objects to this list?
EDIT: There are no server side errors, the code compiles and runs just fine, so this probably is some jquery/ajax error.
This is the ajax code for my debugger:
86 $.ajax({
87 url: $('#url').val(),
88 type: $('#type option:selected').val(),
89 contentType: "application/json; charset=utf-8",
90 dataType: "json",
91 data: $('#data').val(),
92 success: function (result){
93 console.log(result)
94 },
95 error: function (err){
96 console.log(err)
97 }
98 });
Here is the complete method, but the method seems to be OK, it is the ajax or something to do with jquery I guess.
[OperationContract]
public returnData listChildren(string folderId)
{
returnData data = new returnData();
List<object> myList= new List<object>();
bool status = true;
try
{
string Json = Response("https://www.googleapis.com/drive/v2/files/" + folderId + "/children", "GET");
//get the children list
if (!Json.Contains("error"))
{
DriveChildlist list = JsonConvert.DeserializeObject<DriveChildlist>(Json);
foreach (DriveChildren child in list.items)
{
string uriString = child.childLink;
//get the info for each child in the list
Json = Response(uriString, "GET");
if (!Json.Contains("error"))
{
DriveFile file = JsonConvert.DeserializeObject<DriveFile>(Json);
file f = new file();
if (file.mimeType == "application/vnd.google-apps.folder")
{
f.Type = "folder";
f.ID = file.id;
f.Name = file.title;
data.data.Add(f);
}
else if (file.mimeType == "application/myMimeType")
{
f.Type = "file";
f.ID = file.id;
f.Name = file.title;
data.data.Add(f);
}
}
else
{
status = false;
}
}
}
else
{
status = false;
}
}
catch(Exception)
{
status = false;
}
data.status = status;
if (!status)
{
data.data = null;
//data.data is null if status is false
}
return data;
}