slb2013 says:
$(
'#btnCheckSensorMove'
).click(
function
(id) {
let sensorID = $(
this
).data(
"id"
);
$.get(
'/Network/CheckSensorMove/'
+ id,
function
(data) {
eval(data);
if
(data ==
"Success"
) {
}
else
{
alert(data);
}
});
});
You are doing wrong.
You are passing id to Button click event function which is null.
If you want to pass the Id then you need to first get the id value from the element you set. Then use the 2nd parameter of $.get to pass this to controller.
Refer below example.
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult CheckSensorMove(long? id)
{
return Json("Success", JsonRequestBehavior.AllowGet);
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<button type="button" role="button" id="btnCheckSensorMove" data-id="5">CheckSensorMove</button>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#btnCheckSensorMove').click(function (e) {
$.get('/Home/CheckSensorMove/',
{ id: $(this).data('id') },
function (data) {
alert(data);
}).fail(function (r) {
alert(r.responseText);
});
});
});
</script>
</body>
</html>