Form Encoded Javascript int [] is not matched using the Binder model

Last night I tried to put together something that I had with MVC2.

Given the following class:

public class RouteSaveViewModel
{
    public string Title { get; set; }
    public string Comments { get; set; }
    public string DepartureDate { get; set; }
    public string ArrivalDate { get; set; }
    public List<int> LocationIds { get; set; }
}

... and the action below:

    [HttpPost]
    public ViewResult SaveRoute(RouteSaveViewModel route)
    {
        // yada yada
        return SomethingShiny();
    }

I would like to pass data using $ .ajax or $ .post. The form is multi-step to collect data from the user and submit. The code I use to create location identifiers is as follows:

        var routeStopIds = [];
        $(".route-stop").each(function(){
            routeStopIds.push($(this).attr('data-id'));
        });

I checked that there ids that I expect are in this array through the tools and the violinist in the browser. Finally, to send data, I map the objects, as they did in previous MVC builds:

        $.ajax({
            type: 'post',
            url: postUrl,
            data: {
                Title: $("#route-title").val(),
                Comments: $("#route-description").val(),
                DepartureDate: depDate,
                ArrivalDate: arrDate,
                LocationIds: routeStopIds                    
            },
            dataType: 'JSON'
        });

, , LocationIds, . routeStopIds SaveRoute, List<int> , , . , LocationIds null, .

{LocationIds%5b%5d=44&LocationIds%5b%5d=4&LocationIds%5b%5d=2}

... HttpValueCollection - LocationIds[], .

, - ? , ? , ?

+3
1

, , MVC2? , : :

. ASP.NET MVC ( MVC 2.0) ' $.ajax().

ajax . , . - .

        $.ajax({
            type: 'post',
            url: postUrl,
            traditional: true, // <----- here be kittens
            data: {

                Title: $("#route-title").val(),
                Comments: $("#route-description").val(),
                DepartureDate: depDate,
                ArrivalDate: arrDate,
                LocationIds: routeStopIds 
            },
            dataType: 'JSON'
        });
+4

All Articles