I work on asp.net razor page . i face issue when sent list of id from jQuery ajax to razor
page it display on razor page as count 0 although it display on console of browser two items
on array
C#
public JsonResult OnGetListIdUpdated(List selectedIds)
{
return new JsonResult(selectedIds);
}
$(document).ready(function () {
$('#reprintdatabtn').click(function (event) {
event.preventDefault();
var selectedIds = [];
$("input[name='chkSel']:checked").each(function() {
var id = $(this).closest("tr").find("td:last").text();
if (id !== "") {
selectedIds.push(parseInt(id));
}
});
console.log(selectedIds);
$.ajax({
url: '?handler=ListIdUpdated',
type: "GET",
dataType: "json",
data: { selectedIds: selectedIds },
success: function (response) {
$("#lblRowsCount").html(response.counter);
$('#msgstatus').text(response.messageStatus);
// Handle the server response here
}
});
});
});so on browser console.log(selectedIds); i get data correctly as below
Code
[
16810,
16811
]and when debug this line below on razor it display null
C#
public JsonResult OnGetListIdUpdated(List selectedIds)so why this issue happen please

Prasad RaveendranRank #320 · 7.8K reputationPosted Jun 20, 2023, 8:52 PM EDT
Request Format: By default, jQuery's $.ajax method sends the data as query parameters in the URL for GET requests. However, in Razor Pages, parameters are typically expected in the request body for non-GET requests. To resolve this, you can change the type in your AJAX request to "POST" instead of "GET".
$.ajax({ url: '?handler=ListIdUpdated', type: "POST", // Change the type to POST dataType: "json", data: { selectedIds: selectedIds }, success: function (response) { // Handle the response } });Then, update the OnGetListIdUpdated method to use the [HttpPost] attribute to handle the POST request.
ahmed salahRank #2,240 · 628 reputationPosted Jun 20, 2023, 8:59 PM EDT
can you help me please by provide full solutions as razor and jquery ajax because i change it to post and not worked
Deepak RawatRank #646 · 3.7K reputationPosted Jun 21, 2023, 12:56 AM EDT
The issue you're facing might be related to how you're passing the data from the JavaScript/jQuery code to the Razor page. Here are a few things you can check to troubleshoot the problem:
Make sure the
OnGetListIdUpdatedmethod in your Razor page is decorated with the[HttpGet]attribute. This is necessary to indicate that the method should handle HTTP GET requests.Verify that the
selectedIdsparameter in theOnGetListIdUpdatedmethod has the same name as the parameter in your JavaScript code (selectedIds). Case sensitivity matters in this case.Ensure that the Razor page's URL is correct in the
urlproperty of your AJAX request. It seems that you're using a Razor page handler (?handler=ListIdUpdated), so make sure the handler name matches the one defined in your Razor page.Check if you have any model binding conflicts. If you have a conflicting route or another action method that accepts a GET request with the same parameter name (
selectedIds), it can cause issues. In that case, you may need to use a different parameter name or consider changing the HTTP verb (e.g., to POST).Double-check that the
Listparameter in yourOnGetListIdUpdatedmethod is not decorated with any custom model binders or validation attributes that might interfere with the binding process.By verifying these points, you should be able to identify and resolve the issue with passing the list of IDs from jQuery AJAX to your Razor page.
Vishal JoshiRank #377 · 6.7K reputationPosted Jun 21, 2023, 1:01 AM EDT
Hello Ahmed,
It's because of the list of arrays. you need to stringify an array object. Please try the below code to get it to work.
$.ajax({ url: '?handler=ListIdUpdated', type: "GET", dataType: "json", data: JSON.stringify({ selectedIds: selectedIds }), // Json stringify here success: function (response) { $("#lblRowsCount").html(response.counter); $('#msgstatus').text(response.messageStatus); // Handle the server response here } });Thanks
Amit MohantyRank #112 · 20K reputationPosted Jun 21, 2023, 1:32 AM EDT
Try this
$.ajax({ url: '?handler=ListIdUpdated', type: "POST", dataType: "json", data: { selectedIds: selectedIds }, success: function (response) { // Handle the server response here } });public JsonResult OnPostListIdUpdated([FromBody] List selectedIds) { return new JsonResult(selectedIds); }