5

I'm not sure why this is happening. I have a string array that should be sent to a controller action that's expecting a string array. This is my jQuery:

$.post("/@Model.Controller/@Model.Action", { "choices": ajax }, function (result) {
                $("#label" + "@Model.Id").html(result);
            });

This is my controller action:

public JsonResult UpdateMultipleType(string[] choices)
    {
        return Json(choices);
    }

I took a look in Firebug and in the Post Tab, the headers read:

Parametersapplication/x-www-form-urlencoded
choices[]   Comedy
choices[]   Thriller
choices[]   Action
choices[]   Adventure

I've debugged and confirmed that it is hitting UpdateMultipleType and that the string array "choices" is null when that action is called. The call goes through but since we're returning null, I get a Javascript error after the call completes.

I don't know why my controller action is being sent null when it's clear that there is an array called choices being sent over.

2
  • See it. Commented Jul 18, 2011 at 18:31
  • Although, you can access the choices parameter value by using `Request["choices[]"]´ Commented Jul 18, 2011 at 18:52

3 Answers 3

12

you need to tell jQuery to use the traditional way of building ajax data.

put this in your document ready:

jQuery.ajaxSettings.traditional = true;
Sign up to request clarification or add additional context in comments.

4 Comments

glad i could help, i struggled with this myself for quite a while.
Cool, I found this solution after 40 min of search.
This does not work. I'm using jquery 1.9.1 MVC 4. It still ends up as null in the controller action.
@AndrewGee perhaps they have changed something in jquery, or MVC. This solution is from mvc 2, and jQuery 1.4
3

Javascript:

var data = ["a", "b", "c", "d"]; //sample

//Wrap your data ...
var postData = { choices: data };

$.ajax({
    type: "POST",
    url: "/Test/Index", //Your URL here
    data: postData,
    success: function (result) {
        //your code here
    },
    dataType: "json",
    traditional: true
});

Your controller:

public JsonResult UpdateMultipleType(List<String> choices) [...]

See it.

Comments

-2

You need to decorate the Action with attribute with [HttpGet] and pass the second param for Json as Allowget

[HttpGet]
public JsonResult UpdateMultipleType(string[] choices)
{
   return Json(choices, JsonRequestBehavior.AllowGet);
}

If choices are array can you change the 'Choices' to 'Choices[]'

3 Comments

That shouldn't account for why choices is null when the action is called, though. But you're right, there should be an AllowGet there. I've since added it, but, as expected, that doesn't solve my original problem.
Can you change 'Choices' to 'Choices[]'
That didn't work. In fact, putting [HttpGet] before the action confused my Ajax call and it couldn't locate the action.

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.