ajax调用控制器发布空值 [英] ajax call to controller is posting null value
问题描述
我通过JavaScript获取所选行的id并通过jquery ajax post方法将其传递给控制器。但id始终为null。这是我的代码。
查看
< script type = 文本/ JavaScript的跨度>>
function DeleteVehicle(){
alert( 工作跨度>);
if (确认( 你想要吗?删除车辆: + id)){
// 在任何地方获取ID
var id = $( this ).attr(ID);
$ .ajax({
type: POST ,
contentType: application / json; charset = utf-8,
url:' @ Url.Action(DeleteVehicle,Vehicle)',
数据:{ id:id},
dataType: json,
成功: function (数据){
alert( 成功,将数据发送到控制器);
},
错误: function (数据){
alert( 错误: + data.responseText);
}
});
}
}
$( function (){ //
$( #Delete)。click(DeleteVehicle);
});
< / script>
I am getting id of selected row through JavaScript and passing it to controller via jquery ajax post method.but id is always null.here is my code. 这篇关于ajax调用控制器发布空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
控制器
< pre lang =cs> [HttpPost]
public JsonResult DeleteVehicle( string id)
{
string userName = someUserName跨度>;
bool result = false ;
尝试
{
result = ovehc.RemoveVehicle(id,userName);
}
catch (例外情况)
{
}
return Json( new {result},JsonRequestBehavior.AllowGet);
} ( this ) .attr(id);
.ajax({
type: POST,
contentType: application / json; charset = utf-8,
url:' @ Url.Action(DeleteVehicle,车辆)',
数据:{ id:id} ,
dataType: json,
成功: function (data){
alert( 成功,发送数据到controller);
},
错误: function (data){
alert( 错误或: + data.responseText);
}
});
}
}
( function (){ //
view
<script type="text/javascript">
function DeleteVehicle() {
alert("working");
if (confirm("Do you want to delete vehicle: " + id)) {
//fetch id wherever you are fetching
var id = $(this).attr(id);
$.ajax({
type: "POST",
contentType:"application/json;charset=utf-8",
url: '@Url.Action("DeleteVehicle", "Vehicle")',
data: { "id": id },
dataType:"json",
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);
});
</script>
Controller
[HttpPost]
public JsonResult DeleteVehicle(string id)
{
string userName = "someUserName";
bool result = false;
try
{
result = ovehc.RemoveVehicle(id, userName);
}
catch (Exception ex)
{
}
return Json(new { result }, JsonRequestBehavior.AllowGet);
}
(this).attr(id);
.ajax({
type: "POST",
contentType:"application/json;charset=utf-8",
url: '@Url.Action("DeleteVehicle", "Vehicle")',
data: { "id": id },
dataType:"json",
success: function (data) {
alert("Success, sent data to controller");
},
error: function (data) {
alert("Error: " + data.responseText);
}
});
}
}
(function () {//when the document is ready