Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model binding to a list with Non-Sequential Indices possible in MVC 3?

I'm following the info on http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx from Phil Haack

He talks about Non-Sequential indices:

<form method="post" action="/Home/Create">

<input type="hidden" name="products.Index" value="cold" />
<input type="text" name="products[cold].Name" value="Beer" />
<input type="text" name="products[cold].Price" value="7.32" />

<input type="hidden" name="products.Index" value="123" />
<input type="text" name="products[123].Name" value="Chips" />
<input type="text" name="products[123].Price" value="2.23" />

<input type="hidden" name="products.Index" value="caliente" />
<input type="text" name="products[caliente].Name" value="Salsa" />
<input type="text" name="products[caliente].Price" value="1.23" />

<input type="submit" />
</form>

Is this possible in MVC3 when you use model binding with TextBoxFor?
This is the way to do it with sequentiel indices:

@Html.TextBoxFor(m => m[i].Value)

If it's not possible, is their anything else I can do if my indices will not be sequential?

like image 649
Kevin Avatar asked Nov 14 '22 00:11

Kevin


1 Answers

AFAI understand the non-sequential indices technique would require to add hidden fields for each index value. I'm quite sure that the Html.TextBoxFor helper itself does not generate any additional hidden fields. Probably you could achieve this by manually adding the hidden fields with the non-sequential indices.

like image 180
Tz_ Avatar answered Dec 28 '22 12:12

Tz_