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

How to send parameter with ej.base.Ajax

I want to send parameter for getting grid data. But controller function parameter comes null everytime. How can i do that ?

let ajax = new ej.base.Ajax({
url: '/Client/GetClientDeviceDocuments',
data: JSON.stringify({ clientId }),
type: 'POST',
contentType: 'application/json'
});
ajax.send();
ajax.onSuccess = function (data) {
grid.dataSource = JSON.parse(data);
};
public async Task GetClientDeviceDocuments(string clientId)
{
var token = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User"));
var vm = new GetClientDocumentVM
{
ClientId = Convert.ToInt32(clientId),
FileType = DocumentFileTypeVM.Device,
Token = token
};
return Json(await clientApiService.GetClientDocuments(vm));
}

1 Reply 1 reply marked as answer

RS Rajapandiyan Settu Syncfusion Team January 28, 2021 08:53 AM UTC

Hi Tümer, 

Greetings from Syncfusion support. 

Query: I want to send parameter for getting grid data. But controller function parameter comes null everytime. How can i do that ?.  

Based on your query we could see that you need send an additional parameter to the server by your ajax post. From the shared code we found that the data passing is incorrect from your ajax post.  

In your code example, You're telling your Action (GetClientDeviceDocuments) to expect a variable clientId with string type. yet you're not sending that. Instead of you sending object value (data: JSON.stringify({ clientId })) to the controller. 

To achieve your requirement, you need to change the data property of your AJAX request like below. 

[index.chstml]  
        var clientId = "10248VINET"; 
        let ajax = new ej.base.Ajax({ 
            url: '/Home/GetClientDeviceDocuments', 
            data: JSON.stringify(clientId),  // pass the string value  
            type: 'POST', 
            contentType: 'application/json' 
        }); 
        ajax.send(); 
        ajax.onSuccess = function (data) { 
            grid.dataSource = JSON.parse(data); 
        }; 

[HomeControllers.cs] 
        public IActionResult GetClientDeviceDocuments([FromBody] string clientId) // use the correct data type to deserialize the data 
        { 
            return Json(BigData.GetAllRecords()); 
        } 


Screenshot: 
 


Find the below stackoverflow link for your reference. 

Please get back to us if you need further assistance with this. 

Regards, 
Rajapandiyan S 


Marked as answer
Loader.
Up arrow icon