jQuery ASP.NET MVC AJAX 发布到控制器操作不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22139604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC AJAX post to controller action not working
提问by nam
In my ASP.NET MVC 4 view, the following is not calling controller action. Click event does get trigger because I can see the alert message. But when I put a breakpoint on the controller action in debug mode, the app does't get to that point and nothing happens when I click 'Ok' on the alert message. I am using LINQ to SQL. Similar calls to save and insert controller actions work fine:
在我的 ASP.NET MVC 4 视图中,以下不是调用控制器操作。单击事件确实会触发,因为我可以看到警报消息。但是,当我在调试模式下在控制器操作上设置断点时,应用程序不会到达该点,并且当我在警报消息上单击“确定”时没有任何反应。我正在使用 LINQ to SQL。保存和插入控制器操作的类似调用工作正常:
$('#DeletePOC').click(function () {
if (confirm("This action will delete this POC record permanently. Click OK if you want to delete this record; otherwise, click 'Cancel'")) {
disableButton(['#CancelPOC', '#POC']);
$.ajax({
url: '@Url.Action("POCDelete")', type: "POST", dataType: "json",
data: {
SitePOCID: $('#POCId').val()
},
success: function (data) {
$('#POCStatus').html('<div class="success">POC Removed Successfully.</div>');
},
error: function () {
$('#POCStatus').html('<div class="field-validation-error">Some Error Occured in Removing POC.</div>');
}
});
}
});
Controller: I've tested that during debug, app doesn't get to this action method:
控制器:我在调试过程中测试过,应用程序无法使用此操作方法:
[HttpPost]
public ActionResult POCDelete(int id)
{
db.POC_dsp(id);
return Json("");
}
回答by Anoop Joshi
You should give the data same as the function parameters. In your case, your data name should be id(not SitePocId)
您应该提供与函数参数相同的数据。在您的情况下,您的数据名称应为 id(而不是 SitePocId)
$('#DeletePOC').click(function () {
if (confirm("This action will delete this POC record permanently. Click OK if you want to delete this record; otherwise, click 'Cancel'")) {
disableButton(['#CancelPOC', '#POC']);
$.ajax({
url: '@Url.Action("POCDelete")', type: "POST", dataType: "json",
data: {
id: $('#POCId').val()
},
success: function (data) {
$('#POCStatus').html('<div class="success">POC Removed Successfully.</div>');
},
error: function () {
$('#POCStatus').html('<div class="field-validation-error">Some Error Occured in Removing POC.</div>');
}
});
}
});