ajax调用控制器发布空值 [英] ajax call to controller is posting null value

查看:82
本文介绍了ajax调用控制器发布空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过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>





控制器



< 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 (){ //


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.

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


这篇关于ajax调用控制器发布空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆