DataTable ParamModel sColumns Null
DataTable ParamModel sColumns Null
I am using the jQuery library DataTables in a MVC 3 website with C# code.
The problem I am running into is that the JQueryDataTableParamModel param object comes back with a null value for sColumns. So I never get to see the comma separated list of column names.
Why would that be null when the rest of the grid works great? (paging, sorting, filtering = all work fine...)
Here is the relevant code, let me know if you need to see more:
VIEW
[code]
Index
First Name
Login Name
Module
var asInitVals = new Array();
$(document).ready(function () {
var oTable = $('#myDataTable').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": '<%= Url.Action("AjaxHandler","DataTables") %>'
});
$("thead input").keyup(function () {
/* Filter on the column (the index) of this element */
oTable.fnFilter(this.value, $("thead input").index(this));
});
/*
* Support functions to provide a little bit of 'user friendlyness' to the textboxes in
* the footer
*/
$("thead input").each(function (i) {
asInitVals[i] = this.value;
});
$("thead input").focus(function () {
if (this.className == "search_init") {
this.className = "";
this.value = "";
}
});
$("thead input").blur(function (i) {
if (this.value == "") {
this.className = "search_init";
this.value = asInitVals[$("thead input").index(this)];
}
});
});
[/code]
CONTROLLER
[code]
public ActionResult AjaxHandler(JQueryDataTableParamModel param)
{
var allUsers = _userService.GetUsers();
IEnumerable filteredUsers= allUsers;
//column search
for (int x = 0; x < param.iColumns; x++)
{
if (Convert.ToString(Request["sSearch_" + x]) != "")
filteredUsers = FilterResults(x ,Convert.ToString(Request["sSearch_" + x]), filteredUsers);
}
//main search
if (!String.IsNullOrEmpty(param.sSearch))
filteredUsers = FilterResults(-1, param.sSearch, filteredUsers);
//this is hard bound to columns... got to maake dynamic...
var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
Func orderingFunction = (c => sortColumnIndex == 0 ? c.FirstName : sortColumnIndex == 1 ? c.LoginName : c.Module);
var sortDirection = Request["sSortDir_0"]; // asc or desc
if (sortDirection == "asc")
filteredUsers = filteredUsers.OrderBy(orderingFunction);
else
filteredUsers = filteredUsers.OrderByDescending(orderingFunction);
var displayedUsers = filteredUsers
.Skip(param.iDisplayStart)
.Take(param.iDisplayLength);
var result = from c in displayedUsers
select new[] { c.FirstName, c.LoginName, c.Module };
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = allUsers.Count(),
iTotalDisplayRecords = filteredUsers.Count(),
aaData = result
},
JsonRequestBehavior.AllowGet);
}
[/code]
The problem I am running into is that the JQueryDataTableParamModel param object comes back with a null value for sColumns. So I never get to see the comma separated list of column names.
Why would that be null when the rest of the grid works great? (paging, sorting, filtering = all work fine...)
Here is the relevant code, let me know if you need to see more:
VIEW
[code]
Index
First Name
Login Name
Module
var asInitVals = new Array();
$(document).ready(function () {
var oTable = $('#myDataTable').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": '<%= Url.Action("AjaxHandler","DataTables") %>'
});
$("thead input").keyup(function () {
/* Filter on the column (the index) of this element */
oTable.fnFilter(this.value, $("thead input").index(this));
});
/*
* Support functions to provide a little bit of 'user friendlyness' to the textboxes in
* the footer
*/
$("thead input").each(function (i) {
asInitVals[i] = this.value;
});
$("thead input").focus(function () {
if (this.className == "search_init") {
this.className = "";
this.value = "";
}
});
$("thead input").blur(function (i) {
if (this.value == "") {
this.className = "search_init";
this.value = asInitVals[$("thead input").index(this)];
}
});
});
[/code]
CONTROLLER
[code]
public ActionResult AjaxHandler(JQueryDataTableParamModel param)
{
var allUsers = _userService.GetUsers();
IEnumerable filteredUsers= allUsers;
//column search
for (int x = 0; x < param.iColumns; x++)
{
if (Convert.ToString(Request["sSearch_" + x]) != "")
filteredUsers = FilterResults(x ,Convert.ToString(Request["sSearch_" + x]), filteredUsers);
}
//main search
if (!String.IsNullOrEmpty(param.sSearch))
filteredUsers = FilterResults(-1, param.sSearch, filteredUsers);
//this is hard bound to columns... got to maake dynamic...
var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
Func orderingFunction = (c => sortColumnIndex == 0 ? c.FirstName : sortColumnIndex == 1 ? c.LoginName : c.Module);
var sortDirection = Request["sSortDir_0"]; // asc or desc
if (sortDirection == "asc")
filteredUsers = filteredUsers.OrderBy(orderingFunction);
else
filteredUsers = filteredUsers.OrderByDescending(orderingFunction);
var displayedUsers = filteredUsers
.Skip(param.iDisplayStart)
.Take(param.iDisplayLength);
var result = from c in displayedUsers
select new[] { c.FirstName, c.LoginName, c.Module };
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = allUsers.Count(),
iTotalDisplayRecords = filteredUsers.Count(),
aaData = result
},
JsonRequestBehavior.AllowGet);
}
[/code]
This discussion has been closed.
Replies