asp.net mvc 模型绑定器会以正确的顺序保存已发布的数组吗?

发布于 2024-09-07 14:26:20 字数 1556 浏览 5 评论 0原文

所以我有一个数字数组,我将其发布到一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2024-09-14 14:26:20

通常索引是参数名称的一部分:

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

如果您有一个如下所示的控制器操作:

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

更新:

让我们假设您在查询字符串中有多个具有相同名称的参数。此查询字符串将由 ASP.NET 引擎通过本机 HttpRequest 对象转换为 NameValueCollection ,更准确地说,是一个名为 HttpValueCollection 的自定义实现。此类是 ASP.NET 的内部类,这意味着它没有文档记录,并且可能会随着 .NET 版本的不同而发生变化。用 Reflector 查看它,有 FillFromString 以及它的实现方式似乎保留了顺序。但这是我不会依赖的东西。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文