title: ASP.Net MVC File Upload Post Parameters
body: <p>I am trying to go to a view with a speicifed batchId parameter wrapped in a ViewModel, pick a file to upload, get the uploaded file back and store the file data w/ the associated BatchId value in a database.</p>
  
  <p>When the form is submitted I don't know how to get back the viewmodel and the PostedFileBase so that I can get the BatchId value.</p>
  
  <p>I need the batchId value to associate it with the data I am storing in the database.</p>
  
  <p>I have the following Action Method in my Controller to allow adding new customers to the specified batch by means of a file upload and import:</p>
  
  <pre><code>public ActionResult AddCustomers(int batchId)
  {
      var viewModel = new AddCustomersViewModel() { BatchId = batchId, //other view model properties };
          return View(viewModel);
  }
  </code></pre>
  
  <p>My view is strongly typed to that ViewModel:</p>
  
  <pre><code>Inherits="System.Web.Mvc.ViewPage&lt;TestExcelImport.Areas.Admin.ViewModels.AddCustomersViewModel&gt;
  </code></pre>
  
  <p>and has the following for the file upload:</p>
  
  <pre><code>&lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;
      &lt;h2&gt;AddCustomers  Batch ID : &lt;%:Model.BatchId %&gt;&lt;/h2&gt;
  
      &lt;form action="/Admin/Dashboard/AddCustomers" enctype="multipart/form-data" method="post"&gt;
          &lt;input type="file" id="SourceFile" name="SourceFile" /&gt;
          &lt;br /&gt;
          &lt;input type="submit" value="Send" name="btnUpload" id="Submit1" /&gt;
      &lt;/form&gt;
  
  &lt;/asp:Content&gt; 
  </code></pre>
  
  <p>My HttpPost Action Method is defined as:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult AddCustomers(HttpPostedFileBase SourceFile)
      {
          //int batchId = ??? HOW DO I Get the BatchId
  
              int fileLength = SourceFile.ContentLength; //works!
              // read through SourceFile.InputStream and store it in db
          //need the associated BatchID though    
  
           return RedirectToAction("Index");
      }
  </code></pre>
  
  <p>I have tried adding a AddCustomersViewModel in the HttpPost return method parameter list but it is always null.  I can read/parse the uploaded file just fine, just can't get back which BatchId it was a part of.</p>
  
  <p>Anybody see what I'm doing wrong?</p>
  
  <p>Thanks</p>