Model binding new Datatables 1.10 parameters

Model binding new Datatables 1.10 parameters

shoe788shoe788 Posts: 3Questions: 2Answers: 0

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

  • shoe788shoe788 Posts: 3Questions: 2Answers: 0

    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.

  • isepiseisepise Posts: 14Questions: 4Answers: 1
    edited May 2014 Answer ✓

    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

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    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 a legacy option to the ajax 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

This discussion has been closed.