开发者

Why Html.Hidden won't generate hidden fields with the same name but different value

开发者 https://www.devze.com 2023-04-12 06:29 出处:网络
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...

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=""})%>开发者_StackOverflow社区
<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:

<% 
    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 />
<% } %>


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消