11using System ;
2+ using System . Collections . Generic ;
23using System . Web . Mvc ;
34
45namespace Mvc . JQuery . Datatables
@@ -11,26 +12,83 @@ public class DataTablesModelBinder : IModelBinder
1112 public object BindModel ( ControllerContext controllerContext , ModelBindingContext bindingContext )
1213 {
1314 var valueProvider = bindingContext . ValueProvider ;
15+ int columns = GetValue < int > ( valueProvider , "iColumns" ) ;
16+ if ( columns == 0 )
17+ {
18+ return BindV10Model ( valueProvider ) ;
19+ }
20+ else
21+ {
22+ return BindLegacyModel ( valueProvider , columns ) ;
23+ }
24+ }
1425
15- DataTablesParam obj = new DataTablesParam ( GetValue < int > ( valueProvider , "iColumns" ) ) ;
26+ private object BindV10Model ( IValueProvider valueProvider )
27+ {
28+ DataTablesParam obj = new DataTablesParam ( ) ;
29+ obj . iDisplayStart = GetValue < int > ( valueProvider , "start" ) ;
30+ obj . iDisplayLength = GetValue < int > ( valueProvider , "length" ) ;
31+ obj . sSearch = GetValue < string > ( valueProvider , "search[value]" ) ;
32+ obj . bEscapeRegex = GetValue < bool > ( valueProvider , "search[regex]" ) ;
33+ obj . sEcho = GetValue < int > ( valueProvider , "draw" ) ;
34+
35+ int colIdx = 0 ;
36+ while ( true )
37+ {
38+ string colPrefix = String . Format ( "columns[{0}]" , colIdx ) ;
39+ string colName = GetValue < string > ( valueProvider , colPrefix + "[data]" ) ;
40+ if ( String . IsNullOrWhiteSpace ( colName ) ) {
41+ break ;
42+ }
43+ obj . sColumnNames . Add ( colName ) ;
44+ obj . bSortable . Add ( GetValue < bool > ( valueProvider , colPrefix + "[orderable]" ) ) ;
45+ obj . bSearchable . Add ( GetValue < bool > ( valueProvider , colPrefix + "[searchable]" ) ) ;
46+ obj . sSearchValues . Add ( GetValue < string > ( valueProvider , colPrefix + "[search][value]" ) ) ;
47+ obj . bEscapeRegexColumns . Add ( GetValue < bool > ( valueProvider , colPrefix + "[searchable][regex]" ) ) ;
48+ colIdx ++ ;
49+ }
50+ obj . iColumns = colIdx ;
51+ colIdx = 0 ;
52+ while ( true )
53+ {
54+ string colPrefix = String . Format ( "order[{0}]" , colIdx ) ;
55+ int ? orderColumn = GetValue < int ? > ( valueProvider , colPrefix + "[column]" ) ;
56+ if ( orderColumn . HasValue )
57+ {
58+ obj . iSortCol . Add ( orderColumn . Value ) ;
59+ obj . sSortDir . Add ( GetValue < string > ( valueProvider , colPrefix + "[dir]" ) ) ;
60+ colIdx ++ ;
61+ }
62+ else
63+ {
64+ break ;
65+ }
66+ }
67+ obj . iSortingCols = colIdx ;
68+ return obj ;
69+ }
70+
71+ private DataTablesParam BindLegacyModel ( IValueProvider valueProvider , int columns )
72+ {
73+ DataTablesParam obj = new DataTablesParam ( columns ) ;
1674
1775 obj . iDisplayStart = GetValue < int > ( valueProvider , "iDisplayStart" ) ;
1876 obj . iDisplayLength = GetValue < int > ( valueProvider , "iDisplayLength" ) ;
1977 obj . sSearch = GetValue < string > ( valueProvider , "sSearch" ) ;
2078 obj . bEscapeRegex = GetValue < bool > ( valueProvider , "bEscapeRegex" ) ;
2179 obj . iSortingCols = GetValue < int > ( valueProvider , "iSortingCols" ) ;
2280 obj . sEcho = GetValue < int > ( valueProvider , "sEcho" ) ;
23-
81+
2482 for ( int i = 0 ; i < obj . iColumns ; i ++ )
2583 {
2684 obj . bSortable . Add ( GetValue < bool > ( valueProvider , "bSortable_" + i ) ) ;
2785 obj . bSearchable . Add ( GetValue < bool > ( valueProvider , "bSearchable_" + i ) ) ;
28- obj . sSearchColumns . Add ( GetValue < string > ( valueProvider , "sSearch_" + i ) ) ;
86+ obj . sSearchValues . Add ( GetValue < string > ( valueProvider , "sSearch_" + i ) ) ;
2987 obj . bEscapeRegexColumns . Add ( GetValue < bool > ( valueProvider , "bEscapeRegex_" + i ) ) ;
3088 obj . iSortCol . Add ( GetValue < int > ( valueProvider , "iSortCol_" + i ) ) ;
3189 obj . sSortDir . Add ( GetValue < string > ( valueProvider , "sSortDir_" + i ) ) ;
3290 }
33- return obj ;
91+ return obj ;
3492 }
3593
3694 private static T GetValue < T > ( IValueProvider valueProvider , string key )
0 commit comments