title: Html.BeginForm multipart/form-data File Upload form-group validation
body: <p>I have a cshtml file to Upload files to the server.</p>
  
  <pre><code>@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
  {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary();
      &lt;div class="alert alert-success alert-dismissible" role="alert"&gt;@ViewBag.Message&lt;/div&gt;
  
          &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Upload Data Documents&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true)
          @Html.HiddenFor(model =&gt; model.project_id)
  
          &lt;div class="form-group"&gt;
              &lt;label class="control-label col-md-2"&gt;Some Other File&lt;/label&gt;
              &lt;div class="col-md-10"&gt;
                  &lt;input type="file" name="someOtherFile" class="form-control" /&gt;
                  &lt;span class="field-validation-error" id="spanfilesomeOtherFile"&gt;&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label class="control-label col-md-2"&gt;Results Comparison&lt;/label&gt;
              &lt;div class="col-md-10"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;div class="col-md-4"&gt;
                          &lt;input type="file" name="FileUploadResultsComparison" class="form-control" placeholder=".col-md-4"/&gt;
                          &lt;span class="field-validation-error" id="spanfileResultsComparison"&gt;&lt;/span&gt;
                      &lt;/div&gt;
                      &lt;div class="col-md-4"&gt;
                          @if (ViewData["Project"] != null)
                          { 
                              @Html.DropDownList("resultsComp_project", (SelectList)ViewData["Project"], "Select a Project", new { @class = "form-control", @placeholder = ".col-md-4" })
                          }
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label class="control-label col-md-2"&gt;Memory Usage&lt;/label&gt;
              &lt;div class="col-md-10"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;div class="col-md-4"&gt;
                          &lt;input type="file" name="FileUploadMemoryUsage" class="form-control" /&gt;
                          &lt;span class="field-validation-error" id="spanfileMemoryUsage"&gt;&lt;/span&gt;
                      &lt;/div&gt;
                      &lt;div class="col-md-4"&gt;
                          @if (ViewData["Project"] != null)
                          {
                              @Html.DropDownList("memUsage_project", (SelectList)ViewData["Project"], "Select a Project", new { @class = "form-control", @placeholder = ".col-md-4" })
                          }
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" name="Submit" id="btnSubmit" value="Upload Files" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  }
  </code></pre>
  
  <p>And my Controller is as follows</p>
  
  <pre><code>[HttpPost]
  public ActionResult FileUpload(HttpPostedFileBase file)
  {
      foreach (string upload in Request.Files)
      {
          if (!(Request.Files[upload] != null &amp;&amp; Request.Files[upload].ContentLength &gt; 0)) continue;
  
          HttpPostedFileBase file = Request.Files[upload];
  
          if (ModelState.IsValid)
          {
              if (file == null)
              {
                  ModelState.AddModelError("File", "Please Upload Your file");
              }
              else if (file.ContentLength &gt; 0)
              {
                  int MaxContentLength = 1024 * 1024 * 3; //3 MB
                  string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".pdf" };
  
                  if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
                  {
                      ModelState.AddModelError("File", "Please file of type: " + string.Join(", ", AllowedFileExtensions));
                  }
  
                  else if (file.ContentLength &gt; MaxContentLength)
                  {
                      ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB");
                  }
                  else
                  {
                      var fileName = Path.GetFileName(file.FileName);
                      var path = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);
                      file.SaveAs(path);
                      ModelState.Clear();
                      ViewBag.Message = "File uploaded successfully";
                  }
              }
          }
      }
      return View();
  }
  </code></pre>
  
  <p>The code above works, including the file validation. However i am having trouble with 2 things regarding the "Project" associated with the file.</p>
  
  <ol>
  <li>How to i send/return the Project(id) associated with each file to the controller?</li>
  <li><p>How do i selectively validate the whether the "Project" DropDown is selected for the file?
  for example (if i browse and select a file for  how do i ensure that a corresponding "resultsComp_project" value is selected in the Dropdown? )</p></li>
  <li><p>how do i associate file and project if they are selected for both (i.e, name="FileUploadMemoryUsage" &amp; name="FileUploadResultsComparison") </p></li>
  </ol>
  
  <p>Please note that the Memory Usage information can be null, code will only process a file &amp; project if they are entered.</p>