in short, use a for loop instead of a foreach loop (see the answer here). You need to manually index it
MVC Razor view nested foreach's model
EDIT: added sample code
@for(int i=0; i < Model.ListTwo.Count; i++)
{
@Html.HiddenFor(t => t.ListTwo[i].Id)
}
Okay, for collections that inherit from ICollection, try
@for (int i = 0; i < Model.CollectionThree.Count; i++)
{
@Html.Hidden("CollectionThree[" + i + "].Id", Model.CollectionThree.ElementAt(i).Id)
}
Another edit:
To avoid using the property name, you could do something like
@for (int i = 0; i < Model.CollectionThree.Count; i++)
{
@Html.Hidden(Html.NameFor(t => Model.CollectionThree) + "[" + i + "]." +
Html.NameFor(t =>Model.CollectionThree.ElementAt(i).Id)
,Model.CollectionThree.ElementAt(i).Id )
}
Its inelegant, but it doesn't hardcode the property name.
And then lets take those guys out of the loop
@{
var collectionname = Html.NameFor(t => Model.CollectionThree);
var propname = Html.NameFor(t => Model.CollectionThree.First().Id);
}
@for (int i = 0; i < Model.CollectionThree.Count; i++)
{
@Html.Hidden( collectionname+ "[" + i + "]." + propname ,Model.CollectionThree.ElementAt(i).Id )
}
My apologies for not responding earlier. I logged out as I had other things to do. You also may want to do a null check ie, if count > 0, then assign the propname, otherwise skip the whole thing