Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...