ModelBinding parameter list in MVC

拈花ヽ惹草 提交于 2019-12-06 14:10:44

Disclaimer: I'm the author of the mentioned DataTables.MVC project on GitHub

The problem with DataTables 1.10 (new API) is that it's request structure is not entirely compatible with ASP.NET MVC binding engine.

You can either write your own binder or manually handle every single parameter sent from DataTables inside either your QueryString (GET) or Form (POST). You can override it to support other methods.

The whole ideia behind my project is to relief you from handling request parameters over and over again. Just set the binder and use the model: https://github.com/ALMMa/datatables-mvc

On the very first page from the project on GitHub there's a sample code for both setting the binder and handling column ordering/sorting.


As described on the project, the sorting info sent by DataTables is useful on some cases (direct SQL command) but might not be that good if you're using static lists/enumerations or regular linq (although you can use Dynamic Linq to help on this).


Today I've just commited some new code to help you customize/extend the regular binder or to create your JSON binder in a more friendly way and to help getting filtered/sorted columns right from the model.

Give this a try: https://github.com/ALMMa/datatables-mvc

It's a custom ModelBinder implementation for the new DataTable 1.10 input. I just discovered it yesterday and I'm still working on my implementation. So I can't say how well it works; I haven't reached the point of testing. But it looks good and I plan to use it.

I came accross the same problem a few days ago and the solution is pretty straight forward. You can read here more about it.

You just have to pass in a function as callback for the ajax data property. The callback function returns the data as JSON string:

var opts =
    {
        'ajax'    :
        {
            'url': 'serverSideTableProviderPage',
            'type': 'POST',
            'contentType': 'application/json; charset=utf-8',
            'data':function(data)
             {
                   return data = JSON.stringify(data);
             }
        },
        'pagingType': 'simple',
        [more options ...]
    }
$('table').dataTable(opts);

The data is now send as Json and the ModelBinder will fill in your deep nested object properties. Don't forget to set the contentType to "application/json; charset=utf-8"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!