1

I've tried the following with the List<int> being always null on the Action:

$('td').delegate('.roleoption select', 'change', function() {
    var userRoles = new Array();
    $(this).parent().parent().find('select').each(function() {
        userRoles.push($(this).val());
    });

    console.log(userRoles); // [1, 4, 3]

    var userId = $(this).parent().parent().parent().find('td').first().text();
    console.log(userId); // [2]

    $.ajax({
        url: '@Url.Action("CambiarRol", "Administracion")',
        type: 'POST',
        data: { user: userId, roles: userRoles },
        success: function(result) {
            alert("Worked ok.");
        }
    });
});

And my Action:

public ActionResult CambiarRol(int user, List<int> roles)
{
    // "user" is received correctly. "roles" is null.
    return Json(new object());
}

What datatype does my Action need to have in order to correctly receive the roles variable information?

Also what do I need to return from my Action if I want "result" to have a message or something?

    success: function(result) {
        alert("Worked ok.");
    }
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257
  • possible duplicate? - http://stackoverflow.com/questions/5525053/passing-array-from-jquery-to-mvc-net-controller-giving-null-result-on-controller – Zach Green Feb 01 '12 at 17:31
  • Try use $.toJSON(array). More explanation here: http://stackoverflow.com/questions/3308674/asp-net-mvc-send-javascript-associative-array-to-a-controller – djserva Feb 01 '12 at 17:32
  • @djserva: It's not working and doesn't solve the issue. Any other ideas? – Only Bolivian Here Feb 01 '12 at 17:42
  • Relate but doesn't solve my issue: http://stackoverflow.com/questions/8281288/js-int-array-to-mvc3-controller – Only Bolivian Here Feb 01 '12 at 18:20
  • If you converted roles to JSON in your JavaScript then the type for roles in your action needs to be a string. If you need to get it back into an object in your action then you would use the JavascriptSerializer. – Kevin Junghans Feb 01 '12 at 18:23
  • @KevinJunghans: I just want to pass a collection if those ints, it doesn't matter if there are strings or ints as long as they are received on the controller end. The problem isn't creating the data to send, it's how to have the controller recognize the data it's receiving and binding it to the method variable. – Only Bolivian Here Feb 01 '12 at 18:26
  • I think you missed my point. JSON is serialized Javascript objects. Therefore the data format to use to receive JSON is a string. I was not referring to the types in your collections. Try posting the change you made that djserva suggested and you said did not work, because it may be that it was not implemented as djserva intended. – Kevin Junghans Feb 01 '12 at 18:31

1 Answers1

4

Set this before making the call to post.

jQuery.ajaxSettings.traditional = true;

I found this by just realizing that the Array you're sending is actually sent with a key = "roles[]", that's why you always get null on the controller side.

If you change the above, the key will be "roles" and you'll get your list of ints.

Bayron
  • 56
  • 2