传递集合作为模型,而有序索引 [英] Passing collection as model without ordered index

查看:114
本文介绍了传递集合作为模型,而有序索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的名单,我通过它来查看并正确呈现。

I have list of objects and I'm passing it to view and it is rendered properly.

当我提出这种形式下,我越来越得到同样的模式。一切工作正常。
不幸的是,当我决定使用jQuery动态删除一些记录,所以它看起来像这样

提交表格后,我得到只有2第一项目列表。这也可能是因为索引中的自然顺序(0,1,3而不是0,1,2)的arent。
有什么我可以做轻松地修复它(不使用jQuery来改变输入,未便服务器双面)?我试图改变数组列表或IEnumerable的,但仍然一无所获。我知道我可以收拾一切并发送JSON或刚看完的FormCollection,但我想先在这里问问,看看是否有一些其他的解决方案。

When I submit this form, I'm getting getting the same model. Everything works fine. Unfortunately, when I decide to delete dynamically some record using jquery, so it looks like this After submitting form, I'm getting only list with 2 first items. It's probably, because indexes arent in a natural order (0,1,3 instead of 0,1,2). Is there anything I could do to fix it easily (not using jquery to change inputs, smth server sided)? I've tried to change array to List or Ienumerable but still nothing. I know I could pack everything up and send as json or just read the formCollection, but I'd like to ask here first and see if there is some other solution.

推荐答案

您需要包括的索引属性,它允许你回来后非连续的索引器的输入。该指数的值必须集合索引相匹配。例如:

You need to include an input for the Index property which allows you to post back non consecutive indexers. The value of the index must match the collection indexer. For example

for(int i = 0; i < model.tagList.Count; i++)
{
  @Html.TextBoxFor(m => m.tagList[i].Name);
  <input type="hidden" name="tagList.Index" value="@i" />
}

这篇关于传递集合作为模型,而有序索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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