title: Browser back button and duplicate data entry
body: <p>I follow the PRG pattern to avoid form resubmission problem when page is refreshed. These are the  action metods:</p>
  
  <pre><code>   public ViewResult AddDcoument(){
        .....
        return View();
     }
  
     [HttpPost]
     public RedirectToRouteResult SaveDocument(Document doc){
        //save the document
        return RedirectToAction("ViewDocument",new { docId =doc.DocID});
     }
  
     public ViewResult ViewDocument(string docID){
        Document doc=GetDocumentByID(docID);
        return View(doc);
     }
  </code></pre>
  
  <p>When I hit the submit button the document is saved and I get redirected to ViewDocument action and this helps prevent form resubmission when the page is refreshed. But there's another problem, the problem is I can still click the browser back button, which will take me back to the data entry form with all the previously entered data remaining and I can easily click submit again, which means duplicate entry.
   Now one might say that I need to have something that is unique for an entry that will not let a duplicate to be saved to server. In fact I have it and that is the DocumentID, but it's assigned automatically, not by user input. So how do you handle this case?</p>
  
  <p>UPDATE: I actualy found a way around this issue. As per <a href="http://blog.55minutes.com/2011/10/how-to-defeat-the-browser-back-button-cache/" rel="nofollow">this blog</a> and many other SO threads I need to supply the following Http header in order to turn off browser caching:</p>
  
  <pre><code>Cache-Control: no-cache, max-age=0, must-revalidate, no-store
  </code></pre>
  
  <p>I've tried both the following ways:</p>
  
  <p>The encapsulated, OOP way:</p>
  
  <pre><code>var cache=Response.Cache;
  cache.SetExpires(DateTime.UtcNow.AddDays(-1));
  cache.SetValidUntilExpires(false);
  cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
  cache.SetCacheability(HttpCacheability.NoCache);
  cache.SetNoStore();
  </code></pre>
  
  <p>And by simply appending the header string: </p>
  
  <pre><code>Response.AppendHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store");
  </code></pre>
  
  <p>I tried adding them both to the GET method that renders the form and to the POST method that submits the form. But still the form inputs are repopulated from cache.</p>