使用MVC,如果用户点击了垃圾桶图标,它将POST
视图中的mobileDevice.Id
并调用ConfirmDeactivate
//VIEW
<a onclick="Deactivate(@mobileDevice.Id);" class="show-pointer"><i class="icon-trash"></i>Click here</a>
<script>
function Deactivate(mobileDeviceId) {
alert(mobileDeviceId);
$.ajax({
url: 'MobileDevice/ConfirmDeactivate',
type: 'POST',
contentType: 'application/json;',
data: JSON.stringify({ targetid: mobileDeviceId })});
}
</script>
//controller
[HttpPost]
public ActionResult ConfirmDeactivate(int targetid)
{
//update target
}
我错过了ajax ref或者什么?
或者有没有人有任何其他想法?对此。
更新 我已经尝试过以下答案:
//VIEW
<a href="javascript:void(0);" id="post-btn" data-device-id="@mobileDevice.Id" class="show-pointer"><i class="icon-trash">/i>Click here</a>
<script>
$('#post-btn').on('click', function() {
alert(targetid);
$.ajax({
url: '@Url.Action("ConfirmDeactivate", "MobileDevice")''~/MobileDevice/ConfirmDeactivate',
type: 'POST',
data: { targetid: $(this).data('device-id') }
});
})
</script>
//controller
[HttpPost]
public ActionResult ConfirmDeactivate(int targetid)
{
//update target
}
但这不起作用。试图添加警报,但这不显示
$('#post-btn').on('click', function() {
alert(targetid);
请告知
答案 0 :(得分:-1)
尝试更改您的代码:
<a href="javascript:void(0);" id="post-btn" data-device-id="@mobileDevice.Id" class="show-pointer"><i class="icon-trash"></i>Click here</a>
$('#post-btn').on('click', function() {
$.ajax({
url: '@Url.Action("ConfirmDeactivate", "MobileDevice")', //I believe you should use this instead of 'MobileDevice/ConfirmDeactivate'
type: 'POST',
data: { targetid: $(this).data('device-id') }
});
})
当然,如果定义了jQuery,它应该可以工作。