title: How do I submit a MVC view on load?
body: <p>I have a view that has a form with a single hidden submit button.  I want to submit the form when the page loads.  However, any submit I do in Javascript gives me this error:</p>
  
  <blockquote>
    <p>Unable to get property 'submit' of undefined or null reference</p>
  </blockquote>
  
  <p>Here is my code:</p>
  
  <p>PrintApplication.cshtml</p>
  
  <pre><code>    @using (Html.BeginForm("PrintApplications", "Admin", FormMethod.Post, new { @id = "PrintApplications", @class = "form-horizontal ", @commandname = "ModelContract", @target = "_blank" }))
      {
        &lt;input id="btnPrintApplications" type="submit" class="btn btn-primary" value="PrintApplications" name="submit" style="visibility: hidden" /&gt;
      }
  
  &lt;script&gt;
  
      document.onreadystatechange = function () {
          document.form.submit();
      }
  
  &lt;/script&gt;
  </code></pre>
  
  <p>In place of document.form.submit, I've also tried</p>
  
  <pre><code>var form = document.getElementById("PrintApplications");
  form.submit();
  
  document.forms["PrintApplications"].submit();
  
  this.form.submit();
  </code></pre>
  
  <p>and other variations (with # in front of the form name, for example)</p>
  
  <p>What is wrong with my code.  Is there any other way to submit the page automatically (without the user clicking any buttons)?</p>
  
  <p>Edit: Here's my original code.  It didn't work in IE which is what set off the problem I'm having.  There was no error, but the form didn't submit.  This worked fine in Chrome though.</p>
  
  <pre><code>    $(document).ready(function () {
      $("#btnPrintApplications").click();
      });
  </code></pre>
  
  <p>Edit 2: Here's the code I got to work using the code in the post from @barry</p>
  
  <pre><code>@using (Html.BeginForm("PrintApplications", "Admin", FormMethod.Post, new { @id = "PrintApplications", @class = "form-horizontal ", @commandname = "ModelContract", @target = "_blank" }))
  {
      @*&lt;input id="btnPrintApplications" type="submit" class="btn btn-primary" value="PrintApplications" name="submit" style="visibility: hidden" /&gt;*@
      &lt;div&gt;
          If applications do not appear in a separate tab or window, please click the button below.
      &lt;/div&gt;
      &lt;input id="btnPrintApplications" type="submit" class="btn btn-primary" value="Print Applications" name="print" /&gt;
  }
  
  &lt;script&gt;
    mySubmitButton = document.getElementById("btnPrintApplications");
      mySubmitButton.click();
  &lt;/script&gt;
  </code></pre>
  
  <p>Edit 3: Here's the code using the example from @chris-pratt.</p>
  
  <pre><code>@using (Html.BeginForm("PrintApplications", "Admin", FormMethod.Post, new { @id = "PrintApplications", @class = "form-horizontal ", @commandname = "ModelContract", @target = "_blank" }))
  {
      &lt;div&gt;
          If applications do not appear in a separate tab or window, please click the button below.
      &lt;/div&gt;
      &lt;input id="btnPrintApplications" type="submit" class="btn btn-primary" value="Print Applications" name="print" /&gt;
  }
  
  &lt;script&gt;
      window.onload = function () {
          document.forms['PrintApplications'].submit();
      }
  &lt;/script&gt;
  </code></pre>