title: ASP.NET MVC download csv file through modal popup
body: <p>I'm trying to download a csv file through asp.net mvc controller, but since I need user to make a selection input via dropdown list, I created a bootstrap modal pop-up.</p>
  
  <pre><code>@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { @class = "modal-form" }))
  {
      &lt;div class="modal-body"&gt;
          &lt;div class="form-horizontal"&gt;
              &lt;div class="form-group"&gt;
                  @Html.Label("locationTypeId", new { @class = "control-label col-md-3" })
                  &lt;div class="col-md-4"&gt;
                      @Html.DropDownList("locationTypeId", Model.LocationList, new { @class = "form-control" })
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="modal-footer"&gt;
          &lt;button class="btn" data-dismiss="modal" aria-hidden="true"&gt;Cancel&lt;/button&gt;
          &lt;button class="btn btn-primary" type="submit"&gt;Save&lt;/button&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>Once user clicks Save button, it would post to the controller as such:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Create(DefaultViewModel model)
  {
      if (ModelState.IsValid)
      {
          try
          {
              var repository = new SqlRepository(model);
              // go about creating csv file
  
              // returns byte[]
              var output = repository.CreateShipmentDataUpload();
  
              //DownloadCSV();
              return Json(new { success = true });
          }
          catch (Exception e)
          {
              ModelState.AddModelError("", e.Message);
          }
      }
  
      return PartialView("_Create", model);
  }
  </code></pre>
  
  <p>I'm not sure if I can/should call DownloadCSV() there. Here's the DownloadCSV():</p>
  
  <pre><code>public FileContentResult DownloadCSV(byte[] byteArray)
  {
      return File(byteArray, "text/csv", "report.csv");
  }
  </code></pre>
  
  <p>And here's the ajax success function call:</p>
  
  <pre><code>@section Scripts {
  &lt;script type="text/javascript"&gt;
  
      function bindForm(dialog) {
          $('form', dialog).submit(function () {
              $.ajax({
                  url: this.action,
                  type: this.method,
                  data: $(this).serialize(),
                  success: function (result) {
                      if (result.success) {
                          $('#modalDialog').modal('hide');
                          // Refresh:
                          location.reload();
                      } else {
                          $('#modalContent').html(result);
                          bindForm();
                      }
                  }
              });
              return false;
          });
      }
  &lt;/script&gt;
  }
  </code></pre>
  
  <p>How do I get to download the csv file to work properly?</p>