We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Grid and OData V4 Support

Hi,
I am trying to use the OData V4 support for data binding to a grid that has Search features.
When I pass the search on server the response is the following:

{
  "error":{
    "code":"","message":"The query parameter '$search' is not supported."
  }
}

Am I doing something wrong? The other operations works correctly.
Also there is a support for the PATCH Verb of the Odata Controller when doing an Edit?
I have only some fields of my record on the grid and with the PUT Verb the other data are overwritten with null values.
On the OData Protocol it is suggested to prefer PATCH over PUT for updating a record.

We are using the ASP Net Web API OData V4 implementation.

   Thanks in advance

    Andrea Perazzolo

3 Replies

AS Alan Sangeeth S Syncfusion Team July 14, 2016 11:52 AM UTC

Hi Andrea, 

Query 1: “Search Query is not supported” 

Though ODataV4 has support for handling search queries, WebAPI OData doesn’t have support to handle search query. Please refer the following link. 


You may use OData Service without WebAPI to achieve searching support. 

Query 2: “support for the PATCH Verb of the Odata Controller when doing an Edit” 

You can achieve PATCH verb support by using custom adaptor in Grid DataManager. Please refer the following code example. 
var customAdaptor = new ej.ODataV4Adaptor().extend({ 
            update: function (dm, keyField, value, tableName) { 
                return { 
                    type: "PATCH", 
                    url: dm.dataSource.url.replace(/\/*$/, tableName ? '/' + tableName : '') + '(' + value[keyField] + ')', 
                    data: JSON.stringify(value), 
                    accept: this.options.accept 
                }; 
            } 
        }) 
            $("#Grid").ejGrid({ 
                dataSource: ej.DataManager({ url: "/odata/Orders", adaptor: new customAdaptor() }), 
 
}) 


For your convenience we have created a sample and the same can be downloaded from below link. 


Regards,
Alan Sangeeth S 



JP Jerome Piquot June 6, 2018 05:56 AM UTC

Hi,

You can create a custom query attribute to handle the search command :

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public sealed class EnableQueryWithSearchAttribute : EnableQueryAttribute
{
    public Type ModelType { get; set; }
 
    public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
    {
        HttpRequest request = actionExecutedContext.HttpContext.Request;
        KeyValuePair<string, StringValues> searchParam = request.Query.FirstOrDefault(q => q.Key == "$search");
        if (!string.IsNullOrWhiteSpace(searchParam.Key))
        {
            var queryItems = request.Query.Where(q => q.Key != "$search").ToDictionary(p => p.Key, o => o.Value);
            if (!string.IsNullOrWhiteSpace(searchParam.Value))
                queryItems.Add("search", searchParam.Value);
            request.Query = new QueryCollection(queryItems);
        }
        base.OnActionExecuted(actionExecutedContext);
    }
}
Then in the controller use the custom attribute instead of EnableQuery :

[EnableQueryWithSearch]
public IActionResult Get()
{
	string search = Request.Query.FirstOrDefault(p => p.Key == "$search").Value;
 
	//if (!AuthorizationService.AuthorizeAsync(User, CrudPermissions<TD>.Read).Result)
	//    throw new AppException(T["ODATA Get on {0} is not authorized.", T[typeof(TD).Name]]);
 
	if (search == null)
		return Ok(Service.ODataGet() as IQueryable<TD>);
	else
		return Ok(Service.ODataSearch(search) as IQueryable<TD>);
}
 
Jérôme Piquot
FiveForty
France


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team June 8, 2018 01:09 PM UTC

Hi Jerome,  
 
We can also use the following way to search the table in ODataV4. Refer to the following code.  
 
        public PageResult<Order> Get(ODataQueryOptions opts) 
        { 
 
            var results = db.Orders.AsQueryable(); 
            var count = results.Count(); 
            if (opts.OrderBy != null) 
                results = opts.OrderBy.ApplyTo(results); 
            results = results.OrderBy(ord => ord.OrderID); 
            var allUrlKeyValues = ControllerContext.Request.GetQueryNameValuePairs(); 
            string key = allUrlKeyValues.LastOrDefault(x => x.Key == "$search").Value; 
            if (key != null) 
            { 
 
                results = results.Where(fil => fil.ShipCity.Contains(key) || fil.CustomerID.Contains(key)); 
            } 
            . .  
               . .  
            return new PageResult<Order>(results.ToList(), null, count); 
        } 
 
In this, we have looked for the $search key and used the default where filter query.  
 
Regards, 
Seeni Sakthi Kumar 


Loader.
Up arrow icon