1

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.

here is my code: 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);
       }
2
  • In Delete click function don't pass vehicle Id Commented Mar 28, 2014 at 6:57
  • if i dont pass vehicle id in delete function of contoller how can i get it in?? i have to use it for query purpose Commented Mar 28, 2014 at 7:31

3 Answers 3

2

have you put httppost attribute on action method as below?

[httpPost]
 public ActionResult DeleteVehicle(string id)
        {

            //code
               return Json(id, JsonRequestBehavior.AllowGet);
       }

and in 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

Sign up to request clarification or add additional context in comments.

5 Comments

from where you are getting id? coz in your code ur not passing id to the function "DeleteVehicle" @Wasfa
the id i posted through jquery ajax call,i am using this in controller parameter
I dont understand where how to fetch id.coz i was getting the id of selected row in web grid.please help me to clear my concepts
in my above code I have updated like you can get your id like $(this).id? make a try as above @Wasfa
1

var dataPost should be string, something like var dataPost = '{"id":"' + id + '"}' and additional settings for $.ajax are as follows-->

contentType: "application/json; charset=utf-8",
dataType: "json",

1 Comment

i tried this one but again id string is null in controller
0

try to put [HttpPost] Attribute in your Action and try again.

7 Comments

in you Ajax call add contentType: "application/json" aftr datapost
i added this but id is still null
look $(function () {//when the document is ready $("#Delete").click(DeleteVehicle); }); You don't retreive the id here. you have to pass an id to the method. Maybe $(function () {//when the document is ready $("#Delete").click(DeleteVehicle($('#idText').val()); });
i am getting the id of selected vehicle on button click which is in web grid like this <input type="submit" value="Delete" onclick='DeleteVehicle("+ @item.VehicleId +")' id="#[email protected]"/></text>) how can i pass this id param to DeleteVehicle method..
try @Neel solution just up
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.