非连续收集绑定 [英] Non-sequential collection binding

查看:111
本文介绍了非连续收集绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在MVC视图元素的动态创建的集绑定到MVC 4集,我知道previous版本的要求的元素看起来像这样的最佳方式:

What is the best way to bind a dynamically created collection of elements in an MVC view to a collection in MVC 4. I know previous versions have required that the elements look like this:

<input name="Categories[0].CategoryID" type="hidden" value="123" />
<input name="Categories[0].CategoryName" type="hidden" value="Music" />
<input name="Categories[1].CategoryID" type="hidden" value="456" />
<input name="Categories[1].CategoryName" type="hidden" value="Movies" />

但如果你是动态添加新的元素和删除它们,事情很快得到了序列pretty的。

But when you are dynamically adding new elements and removing them, things get out of sequence pretty quickly.

确实 MVC 4 添加绑定到一个非顺序采集的方式?

Does MVC 4 add a way to bind to a non-sequential collection?

推荐答案

支持非连续采集指标,因为MVC2你只需要拥有一个独立的隐藏字段,将与名的索引模式: Col​​lectionName.Index

Non sequential collection indices supported since MVC2 you just need to have a separate hidden field which will hold the index with the name schema: CollectionName.Index:

例如:

<input type="hidden" name="Categories.Index" value="3" />
<input name="Categories[3].CategoryID" type="hidden" value="123" />
<input name="Categories[3].CategoryName" type="hidden" value="Music" />

<input type="hidden" name="Categories.Index" value="1" />
<input name="Categories[1].CategoryID" type="hidden" value="456" />
<input name="Categories[1].CategoryName" type="hidden" value="Movies" />

您可以在这里找到有关主题的更多信息:模式结合一个列表

You can find more info about the topic here: Model Binding To A List

这篇关于非连续收集绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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