2

I am passing a Javascript array of a collection of strings to a post action on my apicontroller, there is a data being sent ( i know this via fiddler ) but it doesn't seem to actually get to the controller action when i am debugging the code. My code is below, any help would be greatly appreciated.

Edit: Just to clarify, the controller action is called but the List stuffsId has a count of zero when there is a list being passed to it

Ajax/Javascript

my.sendStuffToController = function (stuffIds, completedHandler) {

        $.ajax({
            dataType: 'json',
            type: 'POST',
            url: my.stuffUrl,
            data: {
                stuffIds: stuffIds
            },
            success: function (data) {

                if (completedHandler !== undefined) {
                    completedHandler(stuffIds);
                }
            }
        });
    };

My ApiController Post Action

    // Post 
    public void PostAsync(List<string> stuffIds)
    {
        var clientProxy = this.proxyFactory.CreateClientProxy(Tokens.EndPoint);
        var clientChannel = clientProxy.GetClientChannel();

        try
        {
            clientChannel.DoStuffAsync(stuffIds);
        }
        finally
        {
            clientProxy.TryCloseAbortClientChannel();
        }
    }

2 Answers 2

1

dataType is used to specify the return type of the request. contentType is what you should be using to tell the server what you are sending.

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

3 Comments

changing it from dataType to contentType didnt seem to do anything
@PhilipLoyer Did you set the content type to application/json ?
So it turns out that was part of the problem, that was a key part though. So here is what I did to solve it in totality. I changed public void PostAsync(List<string> stuffIds) to public void PostAsync([FromBody] IEnumerable<string> stuffIds) and in my javascript data: { stuffIds: stuffIds }, to data: JSON.stringify(stuffIds),
0

One of the possible (and most common) problems with passing arrays as post parameters from jQuery is described here.

Short version:

Add

jQuery.ajaxSettings.traditional = true;

before your jQuery ajax() function call, i.e.

jQuery.ajaxSettings.traditional = true;
my.sendStuffToController = function (stuffIds, completedHandler) {

        $.ajax({
            dataType: 'json',
            type: 'POST',
            url: my.stuffUrl,
            data: {
                stuffIds: stuffIds
            },
            success: function (data) {

                if (completedHandler !== undefined) {
                    completedHandler(stuffIds);
                }
            }
        });
    };

Long version:

From some point in time jQuery started appending square brackets to variable name if the variable is array, i.e. in your case it will transmit 'stuffIds[]:value1 stuffIds[]:value2' etc. over the ajax call. ASP.NET MVC does not like that and refuses to accept such parameters. jQuery.ajaxSettings.traditional = true switches this jQuery 'feature' off.

1 Comment

That did remove the brackets, but i am still get a zero count when the apicontroller action fires.

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.