I am posting id of a vehicle to controller from jquery ajax post.Ajax call is calling controller method but that string is always null.I searched a lot on web but any solution is not working.id has value in view but null in controller.please help me.
我正在从jquery ajax post向controller发布车辆的id。Ajax调用是调用controller方法,但该字符串始终为空。我在网上搜索了很多,但是没有任何解决办法。id在视图中有值,在控制器中为空。请帮助我。
here is my code: View
这是我的代码:View
<script type="text/javascript">
function DeleteVehicle(id) {
alert("working");
if (confirm("Do you want to delete vehicle: " + id)) {
// var requestData = { 'vehicleId': vid }
var dataPost={'id':id}
$.ajax({
url: '@Url.Action("DeleteVehicle", "Vehicle")',
type: 'POST',
data: dataPost,
success: function (data) {
alert("Success, sent data to controller");
},
error: function (data) {
alert("Error: " + data.responseText);
}
});
}
}
$(function () {//when the document is ready
$("#Delete").click(DeleteVehicle);
});
controller
控制器
public ActionResult DeleteVehicle(string id)
{
//code
return Json(id, JsonRequestBehavior.AllowGet);
}
3 个解决方案
#1
2
have you put httppost attribute on action method as below?
您是否将httppost属性放在action方法上,如下所示?
[httpPost]
public ActionResult DeleteVehicle(string id)
{
//code
return Json(id, JsonRequestBehavior.AllowGet);
}
and in jquery
在jquery
$(document).ready(function() {
$("#Delete").on("click", function(){
var id = $(this).).attr("id"); ////fetch id wherever you are fetching
alert("working");
if (confirm("Do you want to delete vehicle: " + id)) {
// var requestData = { 'vehicleId': vid }
var dataPost={'id':id}
$.ajax({
url: '@Url.Action("DeleteVehicle", "Vehicle")',
type: 'POST',
data: dataPost,
success: function (data) {
alert("Success, sent data to controller");
},
error: function (data) {
alert("Error: " + data.responseText);
}
});
}
});
for more information have a look at this
有关更多信息,请参阅本文
MVC Deleting record using Javascript pop up
使用Javascript弹出MVC删除记录
#2
1
var dataPost
should be string, something like var dataPost = '{"id":"' + id + '"}'
and additional settings for $.ajax
are as follows-->
var dataPost应该是字符串,比如var dataPost = '{"id:"' + id + '"}'以及$的附加设置。ajax如下- - >
contentType: "application/json; charset=utf-8", dataType: "json",
#3
0
try to put [HttpPost]
Attribute in your Action and try again.
尝试将[HttpPost]属性放入操作中,然后再试一次。
#1
2
have you put httppost attribute on action method as below?
您是否将httppost属性放在action方法上,如下所示?
[httpPost]
public ActionResult DeleteVehicle(string id)
{
//code
return Json(id, JsonRequestBehavior.AllowGet);
}
and in jquery
在jquery
$(document).ready(function() {
$("#Delete").on("click", function(){
var id = $(this).).attr("id"); ////fetch id wherever you are fetching
alert("working");
if (confirm("Do you want to delete vehicle: " + id)) {
// var requestData = { 'vehicleId': vid }
var dataPost={'id':id}
$.ajax({
url: '@Url.Action("DeleteVehicle", "Vehicle")',
type: 'POST',
data: dataPost,
success: function (data) {
alert("Success, sent data to controller");
},
error: function (data) {
alert("Error: " + data.responseText);
}
});
}
});
for more information have a look at this
有关更多信息,请参阅本文
MVC Deleting record using Javascript pop up
使用Javascript弹出MVC删除记录
#2
1
var dataPost
should be string, something like var dataPost = '{"id":"' + id + '"}'
and additional settings for $.ajax
are as follows-->
var dataPost应该是字符串,比如var dataPost = '{"id:"' + id + '"}'以及$的附加设置。ajax如下- - >
contentType: "application/json; charset=utf-8", dataType: "json",
#3
0
try to put [HttpPost]
Attribute in your Action and try again.
尝试将[HttpPost]属性放入操作中,然后再试一次。