2

I am working on angularJS application where I am making post call to send data to API controller everything is being sent to controller apart from array of data.

Here is code to make controller call

this.UpdateJobWithDeadlines = function (JobData) {
    var request = $http({
        method: "POST",
        url: "/api/JobAPI/UpdateJobWithDeadlines",
        data: JSON.stringify(JobData)
    });
    return request;
}

I have checked the JobData object, it's value (indented for easier reading) is:

{
    "jobNum":null,
    "jobName":"fgfg",
    /* snip */
    "StatusDatas":[
        {
            "scId":0,
            "JobNum":9746.030148450296,
            "StatusComment":"03-03-2017 : 1",
            "scTimeStamp":"2017-03-03T15:47:48.174Z",
            "IsNew":0
        },
        {
            "scId":0,
            "JobNum":8527.946898255957,
            "StatusComment":"03-03-2017 : 3",
            "scTimeStamp":"2017-03-03T15:47:49.459Z",
            "IsNew":0
        }
    ],
    /* SNIP */
    "telephone":"9"
}

The controller method signature is as below:

public int UpdateJobWithDeadlines(JobDataWithDeadlines JobData)

And the JobDataWithDeadlines class is as below:

public class JobDataWithDeadlines
{
    public int? jobNum;
    public string jobName;
    /* snip */
    public List<StatusData> StatusDatas;
}

public class StatusData
{
    public int scId;
    public int JobNum;

    public string StatusComment;

    public string scTimeStamp;

    public bool IsNew;
}

But I am getting an empty list for the StatusDatas property of the controller method parameter.

8
  • 1
    have you tried [FormBody ] JobDataWithDeadlines JobData Commented Mar 3, 2017 at 16:01
  • 2
    Side Note: use double for Jobnum ;). Commented Mar 3, 2017 at 16:01
  • i think your json is not correct in the StatusDatas part Commented Mar 3, 2017 at 16:02
  • 1
    Is that a copy/paste error? telephone only has a single set of quotes on the right. Commented Mar 3, 2017 at 16:09
  • 1
    It's usually better to test your api separately from the client. Use rest console or one of the other chrome addons/extensions. Once you know whats required to get your API working... then you'll know it's a JS issue if you have trouble calling it from JS Commented Mar 3, 2017 at 16:14

2 Answers 2

1

The problem is your fields, they all need to be properties instead. The deserialization and serialization for json.net will not bind to fields (out of the box anyways, it is possible with additional configuration / custom mapping code).

public class JobDataWithDeadlines
{
    public int? jobNum {get;set;}
    public string jobName {get;set;}
    /* snip */
    public List<StatusData> StatusDatas {get;set;}
}

public class StatusData
{
    public int scId {get;set;}
    public double JobNum {get;set;}
    public string StatusComment {get;set;}
    public string scTimeStamp {get;set;}
    public bool IsNew {get;set;}
}

Also you should specify the content-type in the http header of the request. In this case set it to application/json

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

Comments

1

You have to add the following properties to the object you pass to the $http method:

this.UpdateJobWithDeadlines = function (JobData) {
    var request = $http({
        method: "POST",
        contentType: "application/json",
        dataType: "json",
        url: "/api/JobAPI/UpdateJobWithDeadlines",
        data: JSON.stringify(JobData)
    });
    return request;
}

You can optionally specify a charset to the content type, i.e.:

contentType: 'application/json; charset=utf-8'

Some people also suggest adding traditional: true to it, but in my experience that is not always what causes this problem.

Comments

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.