MVC3非序列指标和DefaultModelBinder [英] MVC3 Non-Sequential Indices and DefaultModelBinder

查看:191
本文介绍了MVC3非序列指标和DefaultModelBinder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是真的,在MVC 3.0默认的模型绑定能够处理非顺序指数(适用于简单和复杂的模型类型)?我遇到这表明它应该职位,但在我的测试看来,它并没有。

Is it true that the default model binder in MVC 3.0 is capable of handling non-sequential indices (for both simple and complex model types)? I've come across posts that suggest it should, however in my tests it appears that it does NOT.

由于后回值:

items[0].Id = 10
items[0].Name = "Some Item"
items[1].Id = 3
items[1].Name = "Some Item"
items[4].Id = 6
items[4].Name = "Some Item"

和一个控制器方法:

public ActionResult(IList<MyItem> items) { ... }

加载的值只有项目0和1;第4项被忽略。

The only values that are loaded are items 0 and 1; item 4 is simply ignored.

我见过无数的解决方案,以生成定制指数(模型绑定到列表),但他们似乎都瞄准了MVC的previous版本,而且大多数是有点'重手'海事组织。

I've seen numerous solutions to generate custom indices (Model Binding to a List), however they all appear to targeting previous versions of MVC, and most are a bit 'heavy-handed' IMO.

我缺少的东西吗?

推荐答案

我有这个工作,你要记得添加一个索引通用输入隐藏在你引用的文章解释说:

I have this working, you have to remember to add a common indexing hidden input as explained in your referenced article:

使用隐藏的输入 NAME = Items.Index 是关键部分。

The hidden input with name = Items.Index is the key part

<input type="hidden" name="Items.Index" value="0" />
<input type="text" name="Items[0].Name" value="someValue1" />

<input type="hidden" name="Items.Index" value="1" />
<input type="text" name="Items[1].Name" value="someValue2" />

<input type="hidden" name="Items.Index" value="3" />
<input type="text" name="Items[3].Name" value="someValue3" />

<input type="hidden" name="Items.Index" value="4" />
<input type="text" name="Items[4].Name" value="someValue4" />

希望这有助于

这篇关于MVC3非序列指标和DefaultModelBinder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆