Breakpoint does not hit at controller action when called through Ajax call

1.3k Views Asked by At

I am making following AJAX call to a method in controller

var jsonParams = { name: $('#txt_name').val(), address: $('#txt_addr').val() };
var json = JSON.stringify(jsonParams);
var actionUrl = "/AddressController/SaveAddress"; 

alert('all ok');  //alert arrives
$.ajax({      
        url: actionUrl,
        data: json,
        dataType: "json",
        type: 'POST',
        async: true,
        contentType: 'application/json; charset=utf-8',   
        success: function (response) {
            // success message
        }
});

And here is my controller action method

public JsonResult SaveAddress(string name, string address)
{
      //code
}

Here I have put the breakpoint at SaveAddress method however when I tried to debug the code using firebug, the Ajax is called but the breakpoint does not arrive at method. Also it does nothing,.. no response, no success message, as if it is not hitting the action method at all.

What I am doing wrong here. I tried all the suggested answers posted for the similar type of questions but none of them worked.

Please help. Thanks in advance.

2

There are 2 best solutions below

0
zaman On

You can create a viewModel Like that your JsonParams Property,and getting your JSON.stringify from your controller by ViewModel.

Public class TestViewModel
{
  public string name {get;set;}
  public string address{get;set;}
}

public JsonResult SaveAddress(TestViewModel json)
{
  //code
}
0
Ajit Boyite On

Try the below url, as your controller method is accepting two parameters not the json string.

var actionUrl = "/Address/SaveAddress?name=" + $('#txt_name').val() +"&address=" + $('#txt_addr').val();