Posting an ints combobox from a jquery controller to .net mvc 3

I'm having difficulty sending a JavaScript object via jQuery to the .net MVC 3 controller.

My object:

var postData = {
    'thing1' : "whatever",
    'thing2' : "something else",
    'thing3' : [1, 2, 3, 4]
}

My jQuery call:

$.post('<%= Url.Action("Commit", "MassEdit") %>', postData, function (data) {
    // stuff
});

My view model:

public class SubmitThing {
    public string thing1 { get; set; }
    public string thing2 { get; set; }
    public IEnumerable<int> thing3 { get; set; }
}

My controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Commit(SubmitThing changes)
{
    return new EmptyResult();
}

The problem is that my "changes" object, which I have in my controller, has thing1 equal to "independent", thing2 is equal to "something else", but thing3 is equal to zero. Now I want item3 to be my list of integers?

Added: I think this is more of a mapping problem than a serialization problem. In my controller, if I look

HttpContext.Request.Form["thing3[]"]

I get a string with a value of "1,2,3,4". But then again, I would like the display to just work.

Thank!

+5
3

json:

    $.ajax({
        url: '<%= Url.Action("Commit", "MassEdit") %>',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify({'thing1' : "whatever",
    'thing2' : "something else",
    'thing3' : [1, 2, 3, 4]
}),
        contentType: 'application/json; charset=utf-8',
        success: function (data) {

        }
    });

+6

, ?

var thing3 = [1, 2, 3, 4];

thing3 = thing3.join(',');

var postData = {
    'thing1' : "whatever",
    'thing2' : "something else",
    'thing3' : thing3 
}

, $. ajax,

0

- -, List<int> IEnumberable<int> ( :) ...;)

2:

, . JSON.stringify()? , ...

var postData = JSON.stringify({
    thing1 : "whatever",
    thing2 : "something else",
    thing3 : [1, 2, 3, 4]
});
0

All Articles