pass kendo grid DataSourceRequest from javascript to ASP.NET

i use kendo ui grid (for asp.net mvc) without problem if i declare dataSource transport read as url like:

var dataSource = {
   pageSize: 20,
   batch: true,
   serverOperation: true,
   serverPaging: true,
   serverSorting: true,
   serverFiltering: true,
   type: "aspnetmvc-ajax",
   transport:
   {
      read:
      {
         url: '/Home/GetNodes',
         type: "POST",
         dataType: "json",
         data: function ()
         {
            return {'p1': 'some text', 'p2': 'other'}
         }
      }
   }
};

now the problem is that i want do use function mode read (is a software design requirement) but the sintax used to pass request parameters to server differ from use transport type "aspnetmvc-ajax" or not. if i set transport type "aspnetmvc-ajax" the read function is not fired, but if not set transport type "aspnetmvc-ajax" the request parameter is not passed well on server side!

if i watch the post parameters sent via http:

with transport type "aspnetmvc-ajax":

page:1
pageSize:20
sort:Field1-asc
filter:Field1~eq~'value1'

without transport type "aspnetmvc-ajax":

page:1
pageSize:20
sort:[{field:Field1, dir:asc}]
filter:{filters:[{operator:eq, value:value1, field:Field1}], logic:and}

sort and filter value parameters differ from "aspnetmvc-ajax" version and server-side are null!

as workaround i manually parse the request params inside parameterMap as shown on bottom, translating sort and filter value sintax compliance with "aspnetmvc-ajax" version and server side are passed well.

  • there is a best solution?
  • why if i use "aspnetmvc-ajax" transport type the read function is not invoked?
  • if the only solution to use read function is to avoid using "aspnetmvc-ajax" transport type, there is a standard way to format request parameters for use on server side in DataSourceRequest object?

thanks

attually my workaround is:

var dataSource = {
   pageSize: 20,
   batch: true,
   serverOperation: true,
   serverPaging: true,
   serverSorting: true,
   serverFiltering: true,
   transport:
   {
      read: function (options)
      {
         var request = $scope.grdDocuments.dataSource.transport.parameterMap({
             filter: $scope.grdDocuments.dataSource.filter(),
             group: $scope.grdDocuments.dataSource.group(),
             sort: $scope.grdDocuments.dataSource.sort(),
             page: $scope.grdDocuments.dataSource.page(),
             pageSize: $scope.grdDocuments.dataSource.pageSize()
         });

         httpService.GetNodes(request.filter, request.group, request.sort, request.page,    request.pageSize, sFolderId, ... other parameters)
         .then(
         function (args)
         {
             //Verifica esito della richiesta http
             if (!sharedService.CheckHttpRequestResult(args, 'GetNodes'))
             {
                 options.error(null);
                 return;
             }

             options.success(args.data);
         }
         );
      },
      parameterMap: function (data, operation)
      {
         //Trasformazione parametro filter
         if (typeof data.filter !== 'undefined')
         {
            //normale: {filters:[{operator:eq, value:a, field:Affidatario}], logic:and}
            //trasformato: Affidatario~eq~'a'
            var filter = '';
            for (var i = 0; i < data.filter.filters.length; i++)
            {
               if (filter !== '')
                  filter += '~' + data.filter.logic + '~';

               var fieldValue = "'" + data.filter.filters[i].value + "'";

               //Formattazione valore in base al formato della colonna
               var found = model.fields[data.filter.filters[i].field];
               if (typeof found !== 'undefined')
               {
                  if (found.type === 'number')
                     fieldValue = data.filter.filters[i].value;
                  else if (found.type === 'date')
                     fieldValue = "datetime'" + data.filter.filters[i].value.format('Y-m-d') + "T00-00-00'";
               }

               filter += data.filter.filters[i].field + '~' + data.filter.filters[i].operator + '~' + fieldValue;
            }
            data.filter = filter;
         }

         //Trasformazione parametro sort
         if (typeof data.sort !== 'undefined')
         {
            //normale: [{field:Anno, dir:asc}]
            //trasformato: Anno-asc
            var sort = '';
            for (var i = 0; i < data.sort.length; i++)
            {
               if (sort !== '')
                  sort += '~';

               sort += data.sort[i].field + '-' + data.sort[i].dir;
            }
            data.sort = sort;
         }

         var result = JSON.parse(JSON.stringify(data));
         return result;
      }
   }
};

server side:

public JsonResult GetNodes([DataSourceRequest]DataSourceRequest request, string sFolderId, ... other parameters)
{

}

Answers


Just in case someone else stumbles across this post like I did today. This might help you

For me the answer was the line

var data = kendo.data.transports["aspnetmvc-ajax"].prototype.options.parameterMap.call(this, optionsData.data, "read", false);

Need Your Help

Make db column optional or not required so I can create record

ruby-on-rails ruby activerecord

I have a column on a table that may or may not get populated when a new record is created. Let's call it free_shipping_amount and its type integer, and it resides in the shipping_methods table.

How to log webservice requests from ASP.Net-MVC application to external webservice?

c# asp.net asp.net-mvc fiddler webservice-client

I have a webapplication which uses an external webservice to send files and messages. The IIS runs on a Windows2003 Server.