I'm working on an ASP.NET Web Forms application. Using Visual Studio 2012 standard template. I want to use DataTable 1.10 with Ajax calls to the server. Since I'm having troubles with using datatables at all, I start from the begining so I can confirm that when I use the simplier examples (like reading from txt file) it works fine. But now, following the examples from the official site I want to implement real Ajax call to a method and return some data.
I have page Test.aspx with this code there:
<script type="text/javascript">
$(document).ready(function () {
$('#example').dataTable({
"processing": true,
"serverSide": true,
"ajax": "/Table.aspx/TestAjax"
});
});
and a very simple HTML :
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
</tr>
</tfoot>
</table>
All this is taken from the example, also, minimized the number of columns for simplicity.
From an example I took this code for my method:
[WebMethod]
public static string TestAjax(string sEcho)
{
var list = new FormatedList();
list.sEcho = sEcho;
list.iTotalRecords = 1;
list.iTotalDisplayRecords = 1;
var item = new List<string>();
item.Add("Gecko");
list.aaData = new List<List<string>>();
list.aaData.Add(item);
return list.ToString();
}
where FormatedList is a class defined like this:
public class FormatedList
{
public FormatedList()
{
}
public string sEcho { get; set; }
public int iTotalRecords { get; set; }
public int iTotalDisplayRecords { get; set; }
public List<List<string>> aaData { get; set; }
}
So when I run my project in the console I can see Status 200 OK for the call to the Test.aspx/TestAjax but I also get an error from the browser for invalid Json. In the example for the server side code the returning type of the method was the actual class FormatedList but it gave the same result. I can understand that in both cases I'm not returning actual Json for which the browser is givign me an error but yet, I see in examples that people are using approaches like this and it sounds like it's working.
So where is my error, if there is, in the code, and what is the proper way to return data from the Server (using C#) so that I can use DataTable?