Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

asp.net mvc - Passing collection as model without ordered index

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" />
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...