1

I have code like below in ASP.Net MVC4. and I am using Razor engine.

@{
    string sDefaultEnvironId = string.Empty;

} 
<script language="javascript" type="text/javascript" >
    function changeHd() {
        $("#hdSelEnvironmentId").val("1");
    }
</script>
<input type="button" value="ChangeHD" onclick="changeHd();" />
@Html.Hidden("hdSelEnvironmentId", sDefaultEnvironId)

The value of hidden field hdSelEnvironmentId is empty when accessing this view at first time. then it was changed to 1 after I clicked button ChangeHD. But after I pressed F5, the value of hidden field hdSelEnvironmentId is still 1, I expected it with initial empty value instead of 1. Can anyone help me to figure it out ?I just can not understand it. I am using Firefox and Firebug, thanks.

Joe.wang
  • 11,537
  • 25
  • 103
  • 180

2 Answers2

2

Edit As discovered by Andreas here, you can remove this behavior by adding an attribute autocomplete=off to your input:

@Html.Hidden("hdSelEnvironmentId", sDefaultEnvironId, new { autocomplete = "off" })

This effect is not due to the cache -- it's a feature (bug?) of Firefox, that when you refresh a page, the inputs of the page do not seem to get re-loaded from the server.

  • try the same thing in Chrome or IE, you'll see that the value resets to empty
  • clear the cache in Firefox, and you'll notice the value still does not get reset.

So, I'm not sure if there's a workaround, but this issue does seem to be restricted to Firefox.

Community
  • 1
  • 1
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • IE has the option `check for newer version of stored page`, we can use `every time i visit the webpage` to get the reset content. – Joe.wang Nov 23 '12 at 05:53
  • From my personal opinion, Seems Firefox want to give more flexibility to web developer for better performance to parse the HTTP response content? – Joe.wang Nov 23 '12 at 06:04
1

instead of setting it in

 @{
    string sDefaultEnvironId = string.Empty;

}

just initialize it in JavaScript.

 $(document).ready(function(){
     $("#hdSelEnvironmentId").val() = "";
    });
    function changeHd() {
            $("#hdSelEnvironmentId").val() = "";   
            $("#hdSelEnvironmentId").val("1");
        }

i think this will help.. even on page refresh as document loads again the value will be set to empty and then to 1 on button click

Venkata K. C. Tata
  • 5,539
  • 4
  • 22
  • 35