5

In my ASP.NET MVC 4 view, the following is not calling controller action. Click event does get trigger because I can see the alert message. But when I put a breakpoint on the controller action in debug mode, the app does't get to that point and nothing happens when I click 'Ok' on the alert message. I am using LINQ to SQL. Similar calls to save and insert controller actions work fine:

$('#DeletePOC').click(function () {
                if (confirm("This action will delete this POC record permanently. Click OK if you want to delete this record; otherwise, click 'Cancel'")) {
                    disableButton(['#CancelPOC', '#POC']);
                    $.ajax({
                        url: '@Url.Action("POCDelete")', type: "POST", dataType: "json",
                        data: {
                            SitePOCID: $('#POCId').val()
                        },
                        success: function (data) {
                            $('#POCStatus').html('<div class="success">POC Removed Successfully.</div>');
                        },
                        error: function () {
                            $('#POCStatus').html('<div class="field-validation-error">Some Error Occured in Removing POC.</div>');
                        }
                    });
                }
            });

Controller: I've tested that during debug, app doesn't get to this action method:

[HttpPost]
public ActionResult POCDelete(int id)
{
   db.POC_dsp(id);
   return Json("");
}

1 Answer 1

8

You should give the data same as the function parameters. In your case, your data name should be id(not SitePocId)

$('#DeletePOC').click(function () {
            if (confirm("This action will delete this POC record permanently. Click OK if you want to delete this record; otherwise, click 'Cancel'")) {
                disableButton(['#CancelPOC', '#POC']);
                $.ajax({
                    url: '@Url.Action("POCDelete")', type: "POST", dataType: "json",
                    data: {
                        id: $('#POCId').val()
                    },
                    success: function (data) {
                        $('#POCStatus').html('<div class="success">POC Removed Successfully.</div>');
                    },
                    error: function () {
                        $('#POCStatus').html('<div class="field-validation-error">Some Error Occured in Removing POC.</div>');
                    }
                });
            }
        });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to @Anoop, I came to know that the parameter name in the data should be the same as the parameter name in the action method. This means that the @Url.Action(ActionMethodName) would look for the action that has the same parameter name as in the Ajax call. But the error section of my Ajax call above did not display any error. I suppose that has something to do with the class name of the div tag of the error section.

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.