Title: Parameter in web api method call is coming in null
Tags: c#;jquery;asp.net-mvc;asp.net-web-api
Question: Writing a web api project and one of the parameters (which is a json array) on my method is coming into the api null. The jquery I'm making the call with looks like this:

```<script&gt;
    $(document).ready(function () {
        $('#btnSubmit').click(function () {
            var jsRequestAction = {
                appId: 'appName',
                custId: 'custId',
                oprId: 'oprId',
                businessProcess: 'Requisition',
                action: 'Approve',
                actionKeys: [
                    'blah blah 1',
                    'blah blah 2',
                    'blah blah 3'
                ]                    
            };

            $.ajax({
                type: "POST",
                content: "json",
                url: "http://localhost/api/appName/custId/oprId",                    
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ requestAction: jsRequestAction })
            });
        });
    });
</script&gt;
```

My web api method looks like this:

```public IList<ResponseAction&gt; ActionCounter(string appName, string custCode, string custUserName, RequestAction requestAction)
    {
        IList<ResponseAction&gt; actionResponseList = new List<ResponseAction&gt;();
        var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        conn.Open();

        try
        {
            foreach (string s in requestAction.actionKeys)
            {
                var command = new SqlCommand
                {
                    CommandText = "Sql statement",
                    Connection = conn
                };
                command.ExecuteNonQuery();

                var reply = new ResponseAction();
                reply.responseActionKey = s;
                reply.responseMessage = "Success";
                actionResponseList.Add(reply);
            }
            return actionResponseList;
        }
        finally
        {
            conn.Close();
            conn.Dispose();
        }
    }
```

RequestAction model:

```public class RequestAction
{

    public string appId { get; set; }
    public string custId { get; set; }
    public string oprId { get; set; }
    public string businessProcess { get; set; }
    public string action { get; set; }
    public string[] actionKeys { get; set; }
    public string actionKey { get; set; }
}
```

When I debug, I step through the method and when I get to the foreach loop, I get a null object reference. Looking in my locals section, all my properties for requestAction are null. I have tried prefixing the object with the [FromBody] tag to no avail after reading a few related articles. Any help would be appreciated.


Here is the accepted answer: I found the answer to my question HERE. It was a matter of changing this:

```$.ajax({
            type: "POST",
            content: "json",
            url: "http://localhost/api/appName/custId/oprId",                    
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ requestAction: jsRequestAction })
        });
```

to this:

```$.ajax({
            type: "POST",
            content: "json",
            url: "http://localhost/api/appName/custId/oprId",                    
            data: jsRequestAction
        });
```

Otherwise, the data won't bind to my model in the controller and everything will be nulled out.


Here is another answer: You need to make sure the object server side matches the object you are creating client side so that the request can be serialized directly into the object.

So your action method will look like this:

```public IList<ResponseAction&gt; ActionCounter(RequestAction requestAction)
{
    // Do stuff
}
```

Where RequestAction should match the javascript object you are creating.

Comment for this answer: Took out all params except requestAction from the web api method and I'm still getting nulls. I should mention that I am moving this method over from a WCF rest service I built previously that worked, but I'm having the issue in web api.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:12/09/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><jquery><asp.net-mvc><asp.net-web-api>', 'Title': 'Parameter in web api method call is coming in null', 'ViewCount': 1142, 'id': 20480557, 'user_AboutMe': '<p>Fledgling software developer.</p>\n', 'user_AccountId': 2107029.0, 'user_DisplayName': 'Rex_C', 'user_Reputation': 2419.0, 'user_Views': 167.0, 'user_id': 1873140.0}}