0

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>
    $(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>

My web api method looks like this:

public IList<ResponseAction> ActionCounter(string appName, string custCode, string custUserName, RequestAction requestAction)
    {
        IList<ResponseAction> actionResponseList = new List<ResponseAction>();
        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.

Rex_C
  • 2,436
  • 7
  • 32
  • 54

2 Answers2

1

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.

Community
  • 1
  • 1
Rex_C
  • 2,436
  • 7
  • 32
  • 54
0

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> ActionCounter(RequestAction requestAction)
{
    // Do stuff
}

Where RequestAction should match the javascript object you are creating.

Captain John
  • 1,859
  • 2
  • 16
  • 30
  • 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. – Rex_C Dec 09 '13 at 21:08