title: ASP.NET MVC posted file model binding when parameter is Model
body: <p>Is there any way to get posted files (<code>&lt;input type="file" /&gt;</code>) to take part in model binding in ASP.NET MVC <strong>without manually looking at the request context in a custom model binder, and without creating a separate action method which only takes a posted file as input?</strong></p>
  
  <p>I would have thought that this would work:</p>
  
  <pre><code>class MyModel {
    public HttpPostedFileBase MyFile { get; set; }
    public int? OtherProperty { get; set; }
  }
  
  &lt;form enctype="multipart/form-data"&gt;
    &lt;input type="file" name="MyFile" /&gt;
    &lt;input type="text" name="OtherProperty" /&gt;
  &lt;/form&gt;
  
  public ActionResult Create(MyModel myModel) { ... } 
  </code></pre>
  
  <p>But given the above scenario, <strong><code>MyFile</code> isn't even part of the value provider values in the binding context.</strong> (<code>OtherProperty</code> is, of course.) But it works if I do this:</p>
  
  <pre><code>public ActionResult Create(HttpPostedFileBase postedFile, ...) { ... } 
  </code></pre>
  
  <p>So, why does no binding occur when the parameter is a model, and how can I make it work? I have no problem with using a custom model binder, but how can I do this in a custom model binder without looking at <code>Request.Files["MyFile"]</code>? </p>
  
  <p>For consistency, clarity and testability, I'd like my code to provide automatic binding of all properties on a model, including those bound to posted files, without manually inspecting the request context. I am currently testing model binding using <a href="http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx" rel="noreferrer">the approach Scott Hanselman wrote about</a>.</p>
  
  <p>Or am I going about this in the wrong way? How would you solve this? Or is this not possible by design due to the history of separation between Request.Form and Request.Files?</p>