This is a migrated thread and some comments may be shown as answers.
Creating a new datasourcerequest using existing filter
3 Answers 233 Views
This is a migrated thread and some comments may be shown as answers.
Rene
Top achievements
Rank 1
Rene asked on 20 Sep 2015, 06:04 PM

Here's what I'm doing:

 

// client side:

//I get a grid datasource filter

var fs = grid.dataSource.Filter()

// I then stringify it and pass it over to the server via an ajax call

// server side, I get the full filter string

// now i do this:

DataSourceRequest dsr = new DataSourceRequest();

Question:  How do I take that filter string and recreate the full datasource Filters ?

p.s. I have to do it this way - I cannot have a DataSourceRequest in the endpoint parameters passed in.

 

 

3 Answers, 1 is accepted

Sort by
0
Rene
Top achievements
Rank 1
answered on 20 Sep 2015, 10:27 PM

I got this working by doing the following:

 // client side:

var bIncludeGridFilter = true;

var otherParam1 = "Hello";

var otherParam2 = "World";

var requestObject = null;

if( bIncludeGridFilter ){

   var grid = $"#SalesGrid").data("kendoGrid");

   var parameterMap = grid.dataSource.transport.parameterMap;

   requestObject = JSON.stringify( parameterMap({ filter: grid.dataSource.filter() }));

}

$.ajax{{

   url: '/Sales/UpdateSales?_otherParam1=' + otherParam1.toString() + '&_otherParam2=' + otherParam2.toString(),

   type: 'post',

   data: requestObject,

   async: false,

   contentType: 'application/json, charset=utf-8',

   cache: false,

   sucess: function( data ){

       grid.dataSource.read();

     }

  });

 

// and on the server side:

 [HttpPost]

public async Task<JsonResult> UpdateSales( [DataSourceRequest] request,  string _otherParam1 = "", string _otherParam2 = ""){

 

     if( request.Filters == null)

        request.Filters = new List<Kendo.Mvc.IFilterDescriptor>();

     if(request.Groups == null)

       request.Groups = new List<Kendo.Mvc.GroupDescriptor>();

     if(request.Sorts == null)

       request.Sorts = new List<Kendo.Mvc.SortDescriptor>();

     if(request.Aggregates == null)

      request.Aggregates =new List<Kendo.Mvc.AggregateDescriptor>();

 

     // here I add something else to the filters....

    request.Filters.Add( new FilterDescriptor() { Member = "SALES_SPECIAL", MemberType = typeof(string), Operator = FilterOperator.IsEqualTo, Value = _otherParam1 });

    // using Entity framework

    DataSourceResult dsr = dbSales.Sales.ToDataSourceResult(request);

    foreach( Sale oSale in dsr.Data ){

        oSale.OtherSpecial = _otherParam2;

    }

    await dbSales.SaveChangesAsync() ;

    return Json( new {success = true}, JsonRequestBehavior.AllowGet);

 

}

 

  

0
Rene
Top achievements
Rank 1
answered on 20 Sep 2015, 10:34 PM

One error above - on the enpoint signature - it should be:

public async Task<JsonResult> UpdateSales( [DataSourceRequest] DataSourceRequest
request,  string _otherParam1 = "", string _otherParam2 = "")​

0
Rene
Top achievements
Rank 1
answered on 20 Sep 2015, 10:40 PM

Revised Solution:

 

// client side:
var bIncludeGridFilter = true;
var otherParam1 = "Hello";
var otherParam2 = "World";
var requestObject = null;
var grid = $"#SalesGrid").data("kendoGrid");
if( bIncludeGridFilter ){ 
   var parameterMap = grid.dataSource.transport.parameterMap;
   requestObject = JSON.stringify( parameterMap({ filter: grid.dataSource.filter() }));
}
$.ajax{{
   url: '/Sales/UpdateSales?_otherParam1=' + otherParam1.toString() + '&_otherParam2=' + otherParam2.toString(),
   type: 'post',
   data: requestObject,
   async: false,
   contentType: 'application/json, charset=utf-8',
   cache: false,
   sucess: function( data ){
       grid.dataSource.read();
     }
 });
// and on the server side:
[HttpPost]
public async Task<JsonResult> UpdateSales( [DataSourceRequest] DataSourceRequest request,  string _otherParam1 = "", string _otherParam2 = ""){
     if( request.Filters == null)
        request.Filters = new List<Kendo.Mvc.IFilterDescriptor>();
     if(request.Groups == null)
       request.Groups = new List<Kendo.Mvc.GroupDescriptor>();
     if(request.Sorts == null)
       request.Sorts = new List<Kendo.Mvc.SortDescriptor>();
     if(request.Aggregates == null)
      request.Aggregates =new List<Kendo.Mvc.AggregateDescriptor>();
     // here I add something else to the filters....
    request.Filters.Add( new FilterDescriptor() { Member = "SALES_SPECIAL", MemberType = typeof(string), Operator = FilterOperator.IsEqualTo, Value = _otherParam1 });
    // using Entity framework
    DataSourceResult dsr = dbSales.Sales.ToDataSourceResult(request);
    foreach( Sale oSale in dsr.Data ){
        oSale.OtherSpecial = _otherParam2;
    }
    await dbSales.SaveChangesAsync() ;
    return Json( new {success = true}, JsonRequestBehavior.AllowGet);
}​

Tags
Grid
Asked by
Rene
Top achievements
Rank 1
Answers by
Rene
Top achievements
Rank 1
Share this question
or