0

Below is the code which i m trying to send. As you can see, ajax call is made at UI and data 'sub' is passed through. this 'sub' has an array of objects in it. So data is present when it is passed.

UI SIDE

$scope.save = function () {
           var sanit = $scope.gridOptions.rowData;
           var sub = JSON.stringify(sanit);

           $.ajax({
               type: 'POST',
               url: '/api/Pr/PM',
               data: sub,     //this has data in it
               contentType: "application/json"
           }).success(function (response) {
               window.alert("Database updated successfully.");
           })
   };

However, when i debug the code at backend, the parameters is showing as null. i have commented the section showing this is null where the data is showing as null at the start of backend function.

BACKEND C# SIDE

[HttpPost]
    public HttpResponseMessage PM([FromBody] string parameters) //this is null.
    {
        string message = string.Empty;
        try
        {
            var model = JsonConvert.DeserializeObject<List<PODetails>>(parameters);
            message = "Insert Successfull";
            return Request.CreateResponse(HttpStatusCode.OK, message);
        }
        catch (Exception ex)
        {
            message = "Insert fail";
            return Request.CreateErrorResponse(HttpStatusCode.NoContent, message);
        }
    }

Can someone please let me know why it is showing as null value at backend.

Patrick
  • 3,938
  • 6
  • 20
  • 32
  • http://blog.codenamed.nl/2015/05/12/why-your-frombody-parameter-is-always-null/ – Eric J. May 24 '16 at 22:45
  • try changing the name "parameters" to "sub" so that it matches the name you are passing from AJAX – Allen May 24 '16 at 22:53
  • 1
    you mentioned sub is an array of objects and you post method has an string parameter, so you must send something like this: { parameters: "data" } – DanielVorph May 24 '16 at 23:00

2 Answers2

2

You need to ensure the data you're sending via AJAX is an object with a single parameter, which should be named the exact same as the parameter your backend is expecting.

In this case:

$.ajax({
  type: 'POST',
  url: '/api',
  data: JSON.stringify({ parameters: sub }),
  contentType: "application/json"
}).success(function (response) {
  ...
})

Next, if the variable "sub" is an array of objects then you must create a class model server side to match the structure of the data being sent. Then, your API's interface should be expecting a list of that newly created class.

For example:

[HttpPost]
public HttpResponseMessage PM(List<YourClassModel> parameters)
{
  ...
}

Your API should now be able to receive and read the data being sent via the AJAX call above.

Igor Barsi
  • 64
  • 5
1

Take a look at this: Post a json object to mvc controller with jquery and ajax

You are sending a list of objects but trying to recieve it as string. You should change your function parameter from (String parameters) to (List parameters) and change your ajax request according to the link above. That will probably solve your problem. (ps: i couldn't try it myself that's why i said probably :) )

Community
  • 1
  • 1
Onur Keskin
  • 140
  • 10