title: ASP.NET MVC - File Uploading and Model Binding
body: <p>I'm building an essay contest page.  The essay can be written into a textarea or can be uploaded.  I'm new to asp.net mvc and have followed along with Sanderson's Pro ASP.NET MVC Framework.</p>
  
  <p>I'm having trouble with maintaining the posted file on the round-trip from failing field validation. </p>
  
  <p>Reading <a href="https://stackoverflow.com/questions/960687/asp-net-mvc-posted-file-model-binding-when-parameter-is-model">this</a> I named my input file as the same as the model field name and am passing this as an input into the controller action:</p>
  
  <pre><code>[AcceptVerbs(HttpVerbs.Post)]
  public ViewResult SubmitEssay(Essay essay, bool acceptsTerms,
      HttpPostedFileBase essay_filepath,
      HttpPostedFileBase budget_filepath)
  {
      if (!acceptsTerms)
          ModelState.AddModelError("acceptsTerms", 
                                   "You must accept the terms and conditions.");
      else
      {
          try
          {
              // stuff uploaded file values
              if (string.IsNullOrEmpty(essay.essay_filepath) 
                     &amp;&amp; (essay_filepath!= null))
              {
                  essay.essay_filepath = essay_filepath.FileName;
              }
  
              if (string.IsNullOrEmpty(essay.budget_filepath) 
                     &amp;&amp; (budget_filepath != null))
              {
                  essay.budget_filepath = budget_filepath.FileName;
              }
  </code></pre>
  
  <p>This is working, per se, but when I go to validate the other fields, upon round-trip the file upload field no longer contains the upload path, such that when I re-submit, after filling in the required fields, there is no posted file.  </p>
  
  <p>What is the best way to maintain a posted file through a round-trip?  Can you even set the "value" of a &lt;input type="file" /> field?  Should I use two fields (one hidden) and set the file input field using jQuery?</p>
  
  <p>What have others done?</p>
  
  <p>Thanks.</p>