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.

4
  • 1
    It is because of browser cache. Commented Nov 23, 2012 at 5:40
  • @KirillBestemyanov, Thanks, How can I fix it not matter the browser cache enable or not ? Commented Nov 23, 2012 at 5:42
  • @KirillBestemyanov Anyway , How can I disable cache in FireFox. thanks. Commented Nov 23, 2012 at 5:44
  • 1
    possible duplicate of Firefox keeps form data on reload Commented Nov 23, 2012 at 5:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
From my personal opinion, Seems Firefox want to give more flexibility to web developer for better performance to parse the HTTP response content?
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.