views:

66

answers:

1

Hello,

so i've got an array of numbers that i'm posting to an asp.net mvc action that has a list(of integer) parameter and it all works great.
my question is this:

Is it safe to assume that the list(of integer) will have the numbers in the same order as they were in the array i posted?

Thanks for your time!

EDIT:

The data being posted looks like this:

POSTDATA=model.LevelIds=6&model.LevelIds=19&model.LevelIds=16&model.LevelIds=21&model.LevelIds=18

I'm using the Tamper Data firefox add on to see it.

I'm using jQuery, in traditional mode.

EDIT:

the action looks something like this:

public function Create(byval model as thecreateviewmodel) as actionresult

the thecreatviewmodel has a bunch of properties, but the interesting one is...

Public Property LevelIdsAs IList(Of Integer) = New List(Of Integer)

on the client side the view model is build with javascript/jquery:

function NewEntityDataBuilder() {
    var theData = { 
        'model.Name' : $('#Name').val(),
        'model.Description' : $('#Description').val(),
        'model.LevelIds' : $('#LevelIds').val()
    };
    return theData;
}

that function is called from this bit of javascript which basically goes through the and adds all of the things in the list to a drop down list (select control) and selects them all.

$('#LevelIds').empty();
$('#AddedLevels').children().each(function () {
$('#LevelIds').append("<option value='" + $(this).attr('LevelId') + "'>" + $(this).attr('LevelId') + "</option>");
        });

$('#LevelIds').children().attr('selected', 'selected');  //select all the options so they get posted.

var dataToPost = NewEntityDataBuilder();   

this seems fairly convoluted when it's put this way, but it's actually fairly simple. it's all part of 2 connected drag and drop lists that are part of a form.

so: if i put the value of a select list with all of it's options selected in a variable and post that to an ilist(of integer) will ilist have them in the same order as they were in the select. It SEEMS like they are. but is that just a coincidence?

A: 

Usually the index is part of the parameter name:

ints[0]=0&ints[2]=2&ints[1]=1

And if you have a controller action that looks like this:

public ActionResult Index(List<int> ints)
{
    // ints[0] = 0
    // ints[1] = 1
    // ints[2] = 2
    return View();
}

UPDATE:

Let's suppose that you have multiple parameters with the same name in the query string. This query string will be parsed by the ASP.NET engine by the native HttpRequest object be convert into a NameValueCollection and more precisely a custom implementation called HttpValueCollection. This class is internal to ASP.NET which means that it is not documented and it might change with versions of .NET. Looking at it with Reflector there is the FillFromString and the way it is implemented it seems to preserve the order. But this is something I wouldn't rely on.

Darin Dimitrov
i've updated my question with more info on the data format.
Patricia
@Patricia, could you please show the signature of the action method that is receiving this request?
Darin Dimitrov
sure thing. added that and a bunch of other information that may or may not be helpful.
Patricia
@Patricia, please see my update.
Darin Dimitrov
ok cool, thanks :) looks like i'll have to find a more reliable way to get them in order.
Patricia