Model binding new Datatables 1.10 parameters
Model binding new Datatables 1.10 parameters
Also posted on Stackoverflow (http://stackoverflow.com/questions/23502034/model-binding-new-datatables-1-10-parameters)
I am using MVC and want to model bind the ajax send parameters. This was easy in the old way (see here http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part) but now with the new 1.10 format it becomes more complicated.
What is the new model that we should use to bind the ajax parameters?
This question has an accepted answers - jump to answer
Answers
From what I can see this new format is unable to be bound automatically by the MVC model binder. The only way I can see reconciling this is to either manually map through Request.Params[...] or to write a different binder to handle this format.
I use this:
dt mvc
My action then looks like:
[HttpPost]
public ActionResult GetServerData([ModelBinder(typeof(DataTablesBinder))] DataTablesRequest requestModel)
{
// ... do some query work
var query = GetDataQueryable(db, requestModel.Search.Value);
var array = query
.Skip(requestModel.Start)
.Take(requestModel.Length)
.Select(x => new {NAME = x.NAME??""})
.ToArray();
var count = GetDataQueryable(db, requestModel.Search.Value).Count();
return new JsonResult()
{
Data = new DataTablesResponse(
requestModel.Draw,
array,
count,
GetDataQueryable(db, null).Count()),
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
MaxJsonLength = int.MaxValue
};
}
p.s. sorry markdown for codeblocks seem not to work properly
You can trigger the old 1.9 style of parameter submission for server-side processing using
$.fn.dataTable.ext.legacy.ajax = true;
. I might also look at adding alegacy
option to theajax
option.You could also try using jQuery's
transitional
Ajax option which, I understand, issued to format the variables in a way .NET understands.Allan