I am trying to generate a hidden list, so I use hidden fields with same name, however Html.Hidden outputs the existing value instead of new one. So this code…
<%
for (int i = 0; i < Model.ProductIds.Count; i++)
{ %>
<%: Html.Hidden("ProductIds", Model.ProductIds[i], new { id=""})%>
<br />
Iteration:<%:i %>
Guid:<%:Model.ProductIds[i]%>
<br />
<% } %>
generates this HTML
<input name="ProductIds" type="hidden" value="48906f4c-1719-43ab-9d7e-c336a71b8624">
<br>
Iteration:0
Guid:48906f4c-1719-43ab-9d7e-c336a71b8624
<br>
<input name="ProductIds" type="hidden" value="48906f4c-1719-43ab-9d7e-c336a71b8624">
<br>
Iteration:1
Guid:b4f01496-dddf-41f2-a05b-43392d779a44
<br>
Note how even though the ids are different, the generated hidden fields got the same value. Why is this happening, and is there any way to work this around?
I’m not sure why this is happening, but an easy workaround is to just construct the html yourself: