Monday, May 25, 2020

Bind a list of non-sequential indexes

I recently discovered a bug in an application. There was a list of items in a form, each with an index (0, 1, 2, 3...) and if I removed item 2 only items 0 and 1 would get posted.

This seemed to me a rather common problem and something more people would have encountered by now. Luckily, I came across this post from 2008 by Phil Haack (https://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/) in which he basically says we need a hidden input with the Index suffix for each item we need to bind to the list.

And guess what? It works!!

No comments: