MVC 4列表模型绑定如何工作?

如果我想将一组形式的输入绑定到ListMVC 4中的,我知道下面的input name属性命名约定将起作用:


<input name="[0].Id" type="text" />

<input name="[1].Id" type="text" />

<input name="[2].Id" type="text" />

但我对模型绑定程序的宽容程度感到好奇。例如,以下内容如何:


<input name="[0].Id" type="text" />

<input name="[3].Id" type="text" />

<input name="[8].Id" type="text" />

模型绑定器将如何处理?它将绑定到List长度为9且为空的a吗?还是仍然绑定到List长度为3的a?还是会完全窒息?


我为什么在乎


我想实现一个动态表格,用户可以在其中向表格添加行,也可以从表格中删除行。因此,如果用户删除了总共8行中的第2行,我想知道是否需要对所有后续输入进行重新编号。


Smart猫小萌
浏览 510回答 3
3回答

慕的地6264312

我有一个看起来像这样的动态列表:<ul id="okvedList" class="unstyled span8 editableList"><li>&nbsp; &nbsp; <input data-val="true" data-val-required="The Guid field is required." id="Okveds_0__Guid" name="Okveds[0].Guid" type="hidden" value="2627d99a-1fcd-438e-8109-5705dd0ac7bb">&nbsp; &nbsp; --//--</li>因此,当我添加或删除行(li元素)时,我必须对项目进行重新排序&nbsp; &nbsp; this.reorderItems = function () {&nbsp; &nbsp; &nbsp; &nbsp; var li = this.el_list.find('li');&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < li.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var inputs = $(li[i]).find('input');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.each(inputs, function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var input = $(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var name = input.attr('name');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.attr('name', name.replace(new RegExp("\\[.*\\]", 'gi'), '[' + i + ']'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var id = input.attr('id');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.attr('id', id.replace(new RegExp('_.*__', 'i'), '_' + i + '__'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };此列表从客户端放入简单的Html.BeginFrom中,就像服务器端的action参数中的List一样
打开App,查看更多内容
随时随地看视频慕课网APP