Search code examples
asp.net-mvcasp.net-mvc-3model-bindingdefaultmodelbinder

Do MVC3 non-sequential hidden input indexes need to come first?


MVC3 non-sequential index hidden inputs for model binding..

<input type="hidden" name="Index" value="whatever" />

Does it matter if they go before, after, in the middle of the other related inputs to be posted?
Does it matter at all where they end up in the posted data?
For example, can they all be lumped together and it still works?

<input type="text" name="[A].Id" value="1" />
<input type="text" name="[B].Id" value="2" />
<input type="hidden" name="Index" value="A" />
<input type="hidden" name="Index" value="B" />

Solution

  • No, the order of your form fields does not matter, nore where they appear on the html page. The most important factor for MVC3 is the name of the fields must match to the name of your controller/action parameter.

    If you have two fields with the same name however, only one value will be returned into your action.