I want to bind a view to a collection of my custom model. This should not be any problem, if I go for the generel way to do this, looping through the model items from my view, and using the bracket syntax when stating the model:
Ie:
@Html.HiddenFor(m => Model[i].Id)
My issue is that I need to do some grouping of my model items, and thus I've made a sub-selection of my items:
@foreach(var itemType in Model.GroupBy(item => item.Type).Select(grp => grp.First()))
{
<p>@itemType:</p>
var selection = Model
.Where(p => p.Type == itemType)
.OrderBy(p => p.CreationDate);
for (int i = 0; i < selection.Count(); i++) {
@Html.HiddenFor(m => selection[i].Id)
@* all my other element bindings here... *@
...
}
Now, the problem is that my controller method, receiving the submitted form, gets an empty model. So the serialization of the model is broken at some point; maybe MVC doesn't like my "selection" variable name?.. Or what could my problem be, and how could I solve it?