MVC3 非序列索引和 DefaultModelBinder [英] MVC3 Non-Sequential Indices and DefaultModelBinder

查看:18
本文介绍了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,而且大多数都有些笨手笨脚"的 IMO.

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天全站免登陆