Creating a new datasourcerequest using existing filter

4 posts, 0 answers
  1. Rene
    Rene avatar
    65 posts
    Member since:
    Nov 2010

    Posted 20 Sep 2015 Link to this post

    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.

     

     

  2. Rene
    Rene avatar
    65 posts
    Member since:
    Nov 2010

    Posted 20 Sep 2015 in reply to Rene Link to this post

    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);

     

    }

     

      

  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Rene
    Rene avatar
    65 posts
    Member since:
    Nov 2010

    Posted 20 Sep 2015 in reply to Rene Link to this post

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

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

  5. Rene
    Rene avatar
    65 posts
    Member since:
    Nov 2010

    Posted 20 Sep 2015 Link to this post

    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);
    }​

Back to Top
UI for ASP.NET MVC is VS 2017 Ready