Unable to parse C# objects to datatables (C#, ASP.NET)
Unable to parse C# objects to datatables (C#, ASP.NET)
Hello !
I have some difficulties with datatables, as I can not give it my data source of C# parsed objects.
My aspx page contains a <table>
with no other properties than an id.
In my C# code, I have this line : return Newtonsoft.Json.JsonConvert.SerializeObject(myObjectList);
which does the job and returns a string of this kind : [ { "property1": "value1", "property2", "value2" },{ "property1": "value3", "property2", "value4" }
In my Javascript code, it is an ajax method which gives me my resulting string :
$.ajax({ type: 'POST', datatype: 'json', success: function (response) { $("#tableId").DataTable({data: response, columns: [{ data: "property1" }, { data: "property2" }] }); } });
It keeps telling me "DataTables warning: table id=DataTables_Table_0 - Requested unknown parameter 'property1' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4"
I also tried with some parsing before sending 'response' in the data field, with no success => {"Uncaught TypeError: Cannot read property 'style' of undefined"`
When parsing my Json string, I saw that my properties lost their double quotes => [ { property1: "value1", property2, "value2" },{ property1: "value3", property2, "value4" }
and I think that's the point because when I force data written by me before parsing, it works...
Do you have any idea of why my parsed Json object loses the double quotes on the properties names ? Is there a way to give Datatables this king of data (without double quotes) ?
Thanks !
This question has an accepted answers - jump to answer
Answers
I think the Ajax success function needs to parse the JSON response before you use it in the Datatables init code. You might need something like this:
response = JSON.parse(response);
Or you might be able to use the
ajax
option along withajax.dataSrc
with a blank string, see the second example.Kevin
Thanks for your answer, I tried parsing the response, but it does not work either. I will try with the dataSrc and we will see.:)