Passing collection as model without ordered index

2019-01-15 16:33发布

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

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 enter image description here 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.

1条回答
▲ chillily
2楼-- · 2019-01-15 17:15

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" />
}
查看更多
登录 后发表回答