Dear team , i am trying to pass a array of values from ajax to controller but i always returing success or 200 despite hitting the controller.
Even with checkpoints it is not hitting
this is my ajax code
JavaScript
$(document).ready(function () {
$('#example').DataTable();
$('#btnSubmit').click(function () {
var ids = [];
$('#example tbody input[name="chkSelect"]:checked').each(function () {
ids.push($(this).attr('id'));
});
console.log(ids);
$.ajax({
type: "POST",
dataType: "JSON",
url: "",
data: { ids: ids },
success: function (data) {
// Handle the response from the controller
}
});
});
});i have modidied the url to empty because i dont have enough reputation to post urls
and this is my conroller
C#
public JsonResult AssignToHR(List ids)
{
foreach (var id in ids)
{
// Do something with the ID
}
return Json(new { success = true });
}i am not sure where i am wrong . please guide.

Jignesh KumarRank #91 · 22.9K reputationPosted Apr 10, 2023, 2:41 AM EDT
Hello,
You need to check url only, Code looks proper just check with url and method parameters both should match then mesthod will hit,
$(document).ready(function () { $('#example').DataTable(); $('#btnSubmit').click(function () { var ids = []; $('#example tbody input[name="chkSelect"]:checked').each(function () { ids.push($(this).attr('id')); }); console.log(ids); $.ajax({ type: "POST", url: "Home/AssignToHR", // This will be controllerName/MethodName data:JSON.stringify(ids), contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { // Handle the response from the controller } }); }); });For more you can refer this link whic will help you out,
https://www.c-sharpcorner.com/blogs/post-the-data-to-asp-net-mvc-controller-using-jquery-ajax
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 10, 2023, 3:17 AM EDT
t seems that the URL in your AJAX code is empty, which means the AJAX request is not being sent to the correct URL. You need to specify the correct URL for the AJAX request to hit the controller action.
Additionally, you might want to check if your controller action is decorated with the
[HttpPost]attribute to ensure that the action is only executed when a POST request is received.Thanks