Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

JavaScript
<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

C#
[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);

        }
Posted
Updated 3-Apr-14 19:57pm
v2

Here is the solution:

you have syntax error at below line. I have fixed this.

JavaScript
data: "{'id':'" + id + "'}",
 
Share this answer
 
Comments
wasfasheikh 4-Apr-14 14:02pm    
this did not solve the problem
Er. Puneet Goel 4-Apr-14 14:08pm    
its working fine for me i checked that. One thing you can do is post your html so that i can verify that if id value is available or ot
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900