Main thing is i am new in Jquery Datatable and from last 2 days I am trying to use it, using json string. But i am not getting require output.
Here is my HTML :
<body>
<div class="container">
<table id="MydataTableDiv">
<thead>
<tr>
<th>NAME</th>
<th>MARKS</th>
</tr>
</thead>
</table>
</div>
</body>
Here is my Script :
$(document).ready(function () {
$("#MydataTableDiv").DataTable({
serverSide: true,
processing: true,
ajax: {
"url": "../Home/GetData",
"dataSrc":''
},
columns: [
{ data: "NAME" },
{ data: "MARKS" }
],
});
});
And finally , My Home Controller :
public JsonResult GetData()
{
DataTable _dt = new DataTable();
_dt.Columns.Add("NAME");
_dt.Columns.Add("MARKS");
for (int i = 0; i < 10; i++)
{
DataRow _dr = _dt.NewRow();
_dr["NAME"] = "A_" + i;
_dr["MARKS"] = i * 10;
_dt.Rows.Add(_dr);
}
string JsonResult = JsonConvert.SerializeObject(_dt);
return Json(new {data=JsonResult }, JsonRequestBehavior.AllowGet);
}
Here is my Json string returned from controller :
[{"NAME":"A_0","MARKS":"0"},{"NAME":"A_1","MARKS":"10"},{"NAME":"A_2","MARKS":"20"},{"NAME":"A_3","MARKS":"30"},{"NAME":"A_4","MARKS":"40"},{"NAME":"A_5","MARKS":"50"},{"NAME":"A_6","MARKS":"60"},{"NAME":"A_7","MARKS":"70"},{"NAME":"A_8","MARKS":"80"},{"NAME":"A_9","MARKS":"90"}]
This code gives me a blank and empty table which contains all paging, filteration options. please help me what is mistake in above code or is there any other way to use it for dynamic data.