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

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

问题描述

我有对象列表,我将它传递给视图,它被正确呈现.

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).有什么我可以做的事情来轻松修复它(不使用 jquery 更改输入,smth 服务器端)?我试图将数组更改为 List 或 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.

推荐答案

您需要为 Index 属性包括一个输入,它允许您回发非连续索引器.索引的值必须与集合索引器相匹配.例如

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