So, you have an MVC form and you’re using the following:
<%= Html.Hidden(“Command”, Model.Command) %>
<%= Html.HiddenFor(m => m.Command) %>
You are doing postbacks to the form, and you’re changing the value of Command in the controller, but for some reason, the value being stored in the hidden field is the old value, not the changed value from said controller. I have been pulling my hair out trying to figure out why. I finally found the answer on stackoverflow.com.
It turns out this is by design, even though Haacked himself doesn’t necessarily like it. The reason is that the helper methods will pull the values from the model state, not from your passed model. This is the correct behavior when dealing with textboxes and other user-changeable fields, but it seems wrong to do this with hidden fields, and Phil sort of agrees in the post above. It would be a breaking change, so I doubt it will ever be “corrected”. You need to build up your own hidden field instead, as shown below and in the post above.
<input type="hidden" name="the-name" value="<%= Html.AttributeEncode(Model.Value) %>" />