Sending custom parameters to the data request

Hi,
I faced a problem while sending custom parameters to remote data source. Here is my grid
     <ejs-grid id="gridNotification" allowPaging="true" allowFiltering="false" allowSorting="false" allowGrouping="true"
              query="new ej.data.Query().addParams('p1', 'v1')">
        <e-datamanager url="/api/notification/datasource" insertUrl="/api/notification/insert"
                       updateUrl="/api/notification/update" removeUrl="/api/notification/delete"
                       crossDomain="true" adaptor="UrlAdaptor">e-datamanager>
        <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Dialog" showConfirmDialog="true" showDeleteConfirmDialog="true">e-grid-editSettings>
        <e-grid-pageSettings pageCount="5" pageSize="10">e-grid-pageSettings>
        <e-grid-filterSettings type="Menu">e-grid-filterSettings>
        <e-grid-groupsettings showDropArea="false" captionTemplate="#ntfGroupCaptionTemplate" columns="@(new[] {"NotificationTypeID"})">e-grid-groupsettings>
        <e-grid-columns>
            <e-grid-column field="NotificationSubscriptionID" headerText="Subscription ID" isPrimaryKey="true" visible="false">e-grid-column>
            <e-grid-column field="NotificationTypeID" headerText="Notification type ID" visible="false">e-grid-column>
            <e-grid-column field="Name" headerText="Description" template="#ntfDescriptionTemplate" width="200">e-grid-column>
            <e-grid-column field="Menu" headerText="" template="#ntfManageMenuTemplate" width="30">e-grid-column>
            <e-grid-column field="System" headerText="System" template="#ntfSystemTemplate" type="string" width="45">e-grid-column>
            <e-grid-column field="Conditions" headerText="Conditions" template="#ntfFilterTemplate" width="50">e-grid-column>
            <e-grid-column field="Template" headerText="Template" template="#ntfMessageTemplate" width="50">e-grid-column>
            <e-grid-column field="Subscribers" template="#ntfSubscribersTemplate" width="150">e-grid-column>
        e-grid-columns>
    ejs-grid>
My server side code for data source request is:
        [HttpPost]
        [Route("datasource")]
        public ActionResult DataSource([FromBodyRemoteDataSource dm)
        {
            if (!ModelState.IsValid) // IsValid == False, see errors below
            {
                return BadRequest(ModelState);
            }
Where RemoteDataSource class has following declaration.

     public class RemoteDataSource
    {
        [JsonProperty(PropertyName = "requiresCounts", Required = Required.Always)]
        public bool RequiresCounts { get; set; }

        [JsonProperty(PropertyName = "skip", Required = Required.Always)]
        public int Skip { get; set; }

        [JsonProperty(PropertyName = "take", Required = Required.Always)]
        public int Take { get; set; }

        [JsonProperty(PropertyName = "where", Required = Required.Default)]
        public RemoteDataSourceWhere[] Where { get; set; }

        [JsonProperty(PropertyName = "sorted", Required = Required.Default)]
        public RemoteDataSourceSort[] Sort { get; set; }

        [JsonProperty(PropertyName = "group", Required = Required.Default)]
        public string[] Group { get; set; }

        [JsonProperty(PropertyName = "select", Required = Required.Default)]
        public string[] Select { get; set; }

        [JsonProperty(PropertyName = "params", Required = Required.Default)]
        public string Query { get; set; }
    }      

Server side ModelState contains following errors:
  • Unexpected character encountered while parsing value: {. Path 'params', line 1, position 129.
  • After parsing a value an unexpected character was encountered: :. Path 'params', line 1, position 133.
I checked HTTP request body:

POST http://localhost:54495/api/notification/datasource HTTP/1.1
Content-Type: application/json; charset=UTF-8

{"requiresCounts":true,"sorted":[{"name":"NotificationTypeID","direction":"ascending"}],"group":["NotificationTypeID"],"params":{"p1":"v1"},"p1":"v1","skip":0,"take":10}

I recon the problem here is the name of query parameter - params. While JSON deserialization happeninng on server side this name probably considered by .NET as reserved. Can you suggest any workarounds for that problem?

Thanks Iurii

6 Replies

RS Renjith Singh Rajendran Syncfusion Team April 6, 2018 11:29 AM UTC

Hi Yury, 

Thanks for contacting Syncfusion support. 

We have analyzed the problem. We are able to reproduce this problem. To pass an additional parameter to the server side we suggest you to use the same name for the “key” and JSON property name other than the name as “params”. Please refer the code example below, 

    <ejs-grid id="Grid" query="new ej.data.Query().addParams('p1', 'v1')"> 
        ... 
    </ejs-grid> 
 
 
    public class RemoteDataSource 
    { 
        ... 
 
        [JsonProperty(PropertyName = "p1", Required = Required.Default)] 
        public string Query { get; set; } 
    } 

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran. 



YG Yury Gerasimov April 12, 2018 10:19 PM UTC

Hi Renjith,

thank you for reply, I recon that can be a solution in my case. 

Just in case anybody is interested in details, on a web-server side i implemented new class based on RemodeDataSource with fields i am sending as query parameters.

public
 class NotificationRemoteDataSource : RemoteDataSource
    {
        [JsonProperty(PropertyName = "displayMode", Required = Required.Always)]
        public NotificationSubscriptionDisplayMode DisplayMode { getset; }
 
        [JsonProperty(PropertyName = "userId", Required = Required.Default)]
        public int? UserID { getset; }
    }

My WEB API class use this class for deserializing request data
[Route("datasource")]
public ActionResult DataSource([FromBodyNotificationRemoteDataSource dm)

And this is how grid query parameters are specified.
<ejs-grid id="@Model.GridName" allowPaging="true" allowFiltering="false" allowSorting="false" allowGrouping="true"
          query="new ej.data.Query().addParams('displayMode', '@Model.DisplayMode').addParams('userId', '@Model.UserId')"


PS Pavithra Subramaniyam Syncfusion Team April 14, 2018 08:33 AM UTC

Hi Yury, 
You can pass the grid query by defining a DataManager class which will be containing the required parameters. Please refer to the below code example.  
   
public class DataManager 
//extend DataManager Class 
    {        [JsonProperty(PropertyName = "skip", Required = Required.Default)]
        public int skip { getset; }
        [JsonProperty(PropertyName = "take", Required = Required.Default)]        public int take { getset; }    }
public class NotificationRemoteDataSource : RemoteDataSource, DataManager  
    {       .   .   .    } [Route("datasource")]public ActionResult DataSource([FromBodyNotificationRemoteDataSource dm)
{  
 // here we used 'skip' and 'take' queries  
   
return new { Items = data.Skip(dm.skip).Take(dm.take), Count = data.Count() }; 
        } 

Currently, we don't have support for inbuilt DataManager class for server-side actions. However, this support will be available in our Essential Studio Volume 2, 2018 release.
 
 
Regards, 
Pavithra S. 



MO moez April 11, 2019 06:21 PM UTC

Can you please post the same example using asp.net core razor pages ?


MO moez April 12, 2019 02:25 AM UTC

I'm going to answer my own question. It's easier than I thought, all we need to do is set the query property on the grid as follows:
query="new ej.data.Query().addParams('ej2grid', 'true')">

Then, on the OnPostDataSource handler in the PageModel, make sure the parameter has a new property corresponding to the name and type of the parameter.
public JsonResult OnPostDataSource([FromBody]CustomData dm)

For example, if the parameter type is "CustomData", the class definition becomes as follows:
public class CustomData
{
     public bool requiresCounts { get; set; }
     public int skip { get; set; }
     public int take { get; set; }
     public bool ej2grid { get; set; }
}



PS Pavithra Subramaniyam Syncfusion Team April 12, 2019 05:27 AM UTC

Hi moez, 

Thanks for sharing your answer here. 

Please get back to us if you need any assistance. 
 
Regards, 
Pavithra S. 


Loader.
Up arrow icon