I started to use the jquery datatables plugin for asp.net mvc2. I have the following code in the index.aspx page of the view.
$(document).ready(function () {
$('#employeeviews').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/Employee/listEmployees",
"fnServerData": function (sSource, aoData, fnCallback) {
alert("aodata is : "+aoData);
alert("as source is : "+sSource);
$.ajax({
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
});
}
});
});
Now that I have written all these, in the controller, i have an action that returns the partialview.
[HttpPost]
//public ActionResult listEmployees()
public JsonResult listEmployees()
{
if (Request.IsAjaxRequest())
{
Models.EmployeeModel empModel = new Models.EmployeeModel();
//return Json(PartialView("EmployeeList", empModel.getAllEmployees()), JsonRequestBehavior.AllowGet);
//return Json(empModel.getAllEmployees());
return Json(new
{
iTotalRecords = 11,
iTotalDisplayRecords = 3,
aaData = empModel.getAllEmployees()
}, JsonRequestBehavior.AllowGet);
}
else
return null;
}
Now what i get is warning messagebox from jquery. I also tried to use the partial view method to return the partial view.
But I have a problem in this, where exactly can i get the output of the ajax response in the js file so that i can set the output accordingly in the view page, as it is not clear in this process. Also i am planning to handle the ajax and paging requests. When i am done with this, i can move to the other parts.
I am also going to test this with the use of the MVCContrib grid control so that the functionality is clear for me.
DataTables warning (tableid="employeeviews"): Added data (size undefined) does not match known number of columns(5)