Reputation: 4280
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?
Upvotes: 1
Views: 268
Reputation: 8754
What is the purpose of a hidden list or multiple hidden fields with the same name? Couldnt you make it a single hidden field with all the values?
Upvotes: 0
Reputation: 4101
I'm not sure why this is happening, but an easy workaround is to just construct the html yourself:
<%
for (int i = 0; i < Model.ProductIds.Count; i++)
{ %>
<input name="ProductIds" type="hidden" value="<%:Model.ProductIds[i]%>">
<br />
Iteration:<%:i %>
Guid:<%:Model.ProductIds[i]%>
<br />
<% } %>
Upvotes: 1