Bind list with missing items, preserve gaps

98 views Asked by At

How can I post a model list with gaps from a MVC webpage, and have it preserve those gaps when accessing it in server code?

For example, I want to post this:

<input type="hidden" name="ImageUrl.Index" value="0" />
<input type="url" name="ImageUrl[0]" value="example0.png" />
<!---->
<input type="hidden" name="ImageUrl.Index" value="2" />
<input type="url" name="ImageUrl[2]" value="example2.png" />

And receive this in the controller:

public ActionResult Save(Gallery model)
{
    model.ImageUrl[0]; // "example0.png"
    model.ImageUrl[1]; // null
    model.ImageUrl[2]; // "example2.png"
}

I already noticed the "Index" hidden value helps send gaps correctly, but currently I'm receiving a list with 2 items, not 3. I would rather avoid declaring a dummy hidden value.

0

There are 0 answers