asp.net mvc 模型绑定器会以正确的顺序保存已发布的数组吗?
所以我有一个数字数组,我将其发布到一个 ASP.NET MVC 操作,该操作具有一个(整数)参数列表,并且一切都很好。
我的问题是这样的:
可以安全地假设(整数)列表中的数字与我发布的数组中的数字顺序相同吗?
感谢您的宝贵时间!
编辑:
发布的数据如下所示:
POSTDATA=model.LevelIds=6&model.LevelIds=19&model.LevelIds=16&model.LevelIds=21&model.LevelIds=18
我正在使用 Tamper Data firefox add就可以看到它。
我正在以传统模式使用 jQuery。
编辑:
该操作看起来像这样:
public function Create(byval model as thecreateviewmodel) as actionresult
thecreatviewmodel 有一堆属性,但有趣的是......
Public Property LevelIdsAs IList(Of Integer) = New List(Of Integer)
在客户端,视图模型是使用 javascript/jquery 构建的:
function NewEntityDataBuilder() {
var theData = {
'model.Name' : $('#Name').val(),
'model.Description' : $('#Description').val(),
'model.LevelIds' : $('#LevelIds').val()
};
return theData;
}
该函数是从这段 javascript 调用的,基本上遍历并将列表中的所有内容添加到下拉列表(选择控件)并选择全部。
$('#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();
这样说起来似乎相当复杂,但实际上相当简单。它是两个连接的拖放列表的一部分,这些列表是表单的一部分。
所以:如果我将选择列表的值与在变量中选择的所有选项一起放置并将其发布到 ilist(整数),ilist 将使它们的顺序与选择中的顺序相同。看起来确实如此。但这只是巧合吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常索引是参数名称的一部分:
如果您有一个如下所示的控制器操作:
更新:
让我们假设您在查询字符串中有多个具有相同名称的参数。此查询字符串将由 ASP.NET 引擎通过本机 HttpRequest 对象转换为 NameValueCollection ,更准确地说,是一个名为
HttpValueCollection
的自定义实现。此类是 ASP.NET 的内部类,这意味着它没有文档记录,并且可能会随着 .NET 版本的不同而发生变化。用 Reflector 查看它,有FillFromString
以及它的实现方式似乎保留了顺序。但这是我不会依赖的东西。Usually the index is part of the parameter name:
And if you have a controller action that looks like this:
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 theFillFromString
and the way it is implemented it seems to preserve the order. But this is something I wouldn't rely on.